【问题标题】:Sending multipart/form-data via cURL通过 cURL 发送 multipart/form-data
【发布时间】:2016-12-28 08:37:56
【问题描述】:

我有以下 HTML 表单:

<form enctype="multipart/form-data" id="upload_form" role="region" action="remoteUpload2.php?command=FileUpload&amp;type=Files&amp;currentFolder=%2F&amp;langCode=en&amp;hash=8e402b8b9927640d&amp;" method="POST" target="ckf_19">
<input name="upload" type="file">
<input value="Upload Selected File" type="submit">
<input name="cancel" value="Cancel" type="button">
</form>

当我提交它时,如果我执行 print_r($_FILES) 我会得到以下输出:

Array
(
    [upload] => Array
        (
            [name] => logo.png
            [type] => image/png
            [tmp_name] => /tmp/phpvIFI0K
            [error] => 0
            [size] => 12201
        )

)

我想弄清楚的是如何以相同的方式发送文件,仅使用 PHP,将两个不同的系统连接在一起。我可以修改发送脚本,但不能修改接收脚本,所以我需要以预期的格式发送数据。

我有没有办法使用 cURL 将数据发布到这个脚本,这会导致 $_FILES 的类似输出?我有我想在服务器上发送的文件,我只需要弄清楚如何将它发布到接收脚本。

【问题讨论】:

  • 好像你想使用php的CURL上传文件..检查thisthis一次。

标签: php forms curl post


【解决方案1】:

我可以用下面的代码解决这个问题:

// initialise the curl request
$request = curl_init('http://www.example.com/connector.php');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('logo.png') . ';filename=logo-test.png'. ';type=image/png'
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

if(curl_exec($request) === false)
{
    echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
    echo 'Operation completed without any errors<br>';
}

// close the session
curl_close($request);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2015-10-28
    • 2016-11-14
    相关资源
    最近更新 更多