【发布时间】:2014-01-22 14:59:28
【问题描述】:
所以经过一番研究,我发现可以通过cURL将文件发送到另一个页面。这是发送部分的代码:
$url = 'http://someabc.com/api.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$postData = array(
'array' => "@".realpath('array.txt'),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
echo $response;
接收部分(api.php)
if(isset($_POST['array']))
{
$string = $_POST['array'];
echo $string;
}
else
{
echo 'Not found';
}
当我运行包含 cURL 请求的页面时,我在页面上打印了Not found。
这是否意味着我以错误的方式捕获数据?如果是这样,从api.php 页面获取array.txt 内容的方法是什么?
更新
按照 Jon 的建议使用 $_FILES 后,我收到了以下数组:
Array
(
[name] => array.txt
[type] => application/octet-stream
[tmp_name] => /tmp/phpPQZXf9
[error] => 0
[size] => 77413
)
现在我尝试使用以下方法获取此文件的内容:
$tmp = $_FILES['array'];
$string = file_get_contents($tmp['name']['tmp_name']);
但收到错误Warning: file_get_contents(a) [function.file-get-contents]: failed to open stream: No such file or directory,这意味着我没有正确引用文件。我现在哪里出错了?
【问题讨论】:
-
$_FILES数组中的任何内容?因为你正在尝试发送一个文件,所以数据应该放在这个位置而不是$_POST。 -
但是我没有使用
<input type="file" />...只是从目录中提取一个文本文件并发送它。 -
你正在发送一个文件——在接收端也是一样的。
-
好的。我确实得到了一些东西。正在更新答案。
-
您无法从
tmp直接访问该文件,为此,您必须使用move_uploaded_file(至少,通常您不应该这样做。)