【问题标题】:Is it possible to use cURL to stream upload a file using POST?是否可以使用 cURL 通过 POST 流式上传文件?
【发布时间】:2013-04-04 20:09:25
【问题描述】:

或者 PHP 不允许这样做?我已阅读可以使用 PUT,但服务器只需要 POST。

【问题讨论】:

标签: php curl


【解决方案1】:

cURL 已经支持流,试试curl --help | grep binary 你会得到:

--data-binary DATA HTTP POST 二进制数据(H)

一个例子:

curl -v -XPOST http://example:port/path --data-binary @file.tar
-H "Content-Type: application/octet-stream"

【讨论】:

  • @file 会将内容加载到内存中。对大文件不利。请改用 -T 选项。
【解决方案2】:

是的,可以使用带有 cURL 的 POST 文件上传流式上传文件。这是默认设置,您需要提供要以字符串形式流式传输的文件的文件名。

// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "@c:/logs.log";

// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);

// Just for debug: to see response
echo $response;

除了该默认方法之外,不能使用 cURL 通过 POST 方法流式上传文件。

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2019-09-14
    • 2017-07-28
    • 2012-11-07
    相关资源
    最近更新 更多