【问题标题】:Upload files with PHP curl使用 PHP curl 上传文件
【发布时间】:2016-08-18 16:57:52
【问题描述】:

对于我们的客户,我必须创建一个邮件备份系统来备份所有发送的内容(主题、正文、附件、标题...)

但我无法通过 curl 上传文件。

这是我目前的代码:

$filesBackup = array('api_key' => 'xxx', 'files' => array());

                    foreach ($attachment as $att) {

                        $filesBackup['files'][] = new \CURLFile($att[0]);


                    $request = curl_init('http://xxx/webhooks/uploadtos3.php');
                    curl_setopt($request, CURLOPT_POST, true);
                    curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
                    curl_setopt(
                        $request,
                        CURLOPT_POSTFIELDS, http_build_query($filesBackup));
                    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
                    curl_exec($request);
                    curl_close($request);

这里的问题是文件路径($att[0])如下:“http://xxx/uploads/zonnepanelen/file.pdf

所以我在uploadtos3.php 收到的唯一东西就是这个数组:

array(3) {
      ["name"]=>
      string(89) "http://xxx/uploads/zonnepanelen/file.pdf"
      ["mime"]=>
      string(0) ""
      ["postname"]=>
      string(0) ""
    }

有没有人可以帮我解决这个问题?

非常感谢!

【问题讨论】:

  • 请看cURLFile
  • 我认为需要上传的文件应该在文件所在的同一路径

标签: php curl file-upload


【解决方案1】:

用下面的代码更新代码

$filesBackup['files'][]  should contain below keys?

array(
      'file' =>
          '@'            . $_FILES['file']['tmp_name']
          . ';filename=' . $_FILES['file']['name']
          . ';type='     . $_FILES['file']['type']
    )

【讨论】:

  • 我不能使用 $_FILES 因为我收到的文件的唯一引用是这样的路径“xxx.xx/uploads/file.pdf”,我猜 '@'.$file 在 php 5.5 中已弃用?跨度>
  • ok den 只是从链接中获取文件名和类型并添加到数组中。检查并查看
猜你喜欢
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2012-11-24
相关资源
最近更新 更多