duan7772 2014-01-22 14:59
浏览 178
已采纳

获取通过cURL POST发送的文件的内容

So after researching a bit, I found out that it is possible to send a file through cURL to another page. Here is the code that is doing the sending part:

$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;

Receiving Part (api.php)

if(isset($_POST['array']))
{
    $string = $_POST['array'];
    echo $string;
}
else
{
    echo 'Not found';
}

When I run the page containing the cURL request, I'm getting Not found printed on the page.

Does this mean I'm capturing the data in a wrong way? If so, what would be the way to get the contents of array.txt from api.php page?

UPDATE

After using $_FILES as suggested by Jon, I received the following array:

Array
(
    [name] => array.txt
    [type] => application/octet-stream
    [tmp_name] => /tmp/phpPQZXf9
    [error] => 0
    [size] => 77413
)

Now I tried getting the contents of this file using:

$tmp = $_FILES['array'];
$string = file_get_contents($tmp['name']['tmp_name']);

but got the error Warning: file_get_contents(a) [function.file-get-contents]: failed to open stream: No such file or directory which would mean I'm not referencing the file correctly. Where did I go wrong now?

  • 写回答

2条回答 默认 最新

  • douliao5942 2014-01-22 15:42
    关注

    For the first part before the update, the problem exists because when posting a file to a page, regardless of the method in which it happens (ie <input type='file />, curl, etc) it is always available to PHP via the $_FILES variable, which is an associative array, in your case, should be $_FILES['array'] containing the information of the temporary file location. The array should be similar to:

    Array 
    (
        [array] => Array
        (
                [name] => array.txt
                [type] => encoding type
                [tmp_name] => /tmp/path/file
                [error] => 0 (if no error)
                [size] => [filesize]
        )
    )
    

    From there, to access the file, you'd want to move it from the tmp directory to one you have permission to access. This is accomplished with move_uploaded_file. An example of usage with this would be:

    $upDir = 'uploads/';
    move_uploaded_file($_FILES['array']['tmp_name'], $upDir. $_FILES['array']['name']);
    

    From there, the file will be on the server under the relative path to the php file in uploads/array.txt, and you can do what you will with it there. ^^

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥50 内网的网页打开的Excel下载到本地