<tfoot id='FaEar'></tfoot>
    <legend id='FaEar'><style id='FaEar'><dir id='FaEar'><q id='FaEar'></q></dir></style></legend>

    <i id='FaEar'><tr id='FaEar'><dt id='FaEar'><q id='FaEar'><span id='FaEar'><b id='FaEar'><form id='FaEar'><ins id='FaEar'></ins><ul id='FaEar'></ul><sub id='FaEar'></sub></form><legend id='FaEar'></legend><bdo id='FaEar'><pre id='FaEar'><center id='FaEar'></center></pre></bdo></b><th id='FaEar'></th></span></q></dt></tr></i><div id='FaEar'><tfoot id='FaEar'></tfoot><dl id='FaEar'><fieldset id='FaEar'></fieldset></dl></div>

      1. <small id='FaEar'></small><noframes id='FaEar'>

          <bdo id='FaEar'></bdo><ul id='FaEar'></ul>

        PHP 通过 cURL 发送本地文件

        PHP Send local file by cURL(PHP 通过 cURL 发送本地文件)

        • <bdo id='hgC7a'></bdo><ul id='hgC7a'></ul>

            <small id='hgC7a'></small><noframes id='hgC7a'>

            • <tfoot id='hgC7a'></tfoot>

                  <tbody id='hgC7a'></tbody>
                1. <i id='hgC7a'><tr id='hgC7a'><dt id='hgC7a'><q id='hgC7a'><span id='hgC7a'><b id='hgC7a'><form id='hgC7a'><ins id='hgC7a'></ins><ul id='hgC7a'></ul><sub id='hgC7a'></sub></form><legend id='hgC7a'></legend><bdo id='hgC7a'><pre id='hgC7a'><center id='hgC7a'></center></pre></bdo></b><th id='hgC7a'></th></span></q></dt></tr></i><div id='hgC7a'><tfoot id='hgC7a'></tfoot><dl id='hgC7a'><fieldset id='hgC7a'></fieldset></dl></div>

                  <legend id='hgC7a'><style id='hgC7a'><dir id='hgC7a'><q id='hgC7a'></q></dir></style></legend>

                  本文介绍了PHP 通过 cURL 发送本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过客户端 curl 应用程序发送本地文件.我找到了一些示例来处理表单中的文件.就我而言,我没有表单,只有一个本地文件.

                  Im trying to send a local file by client curl app. I found some examples to do that with files from a form. In my case, I have no form, but a local file.

                  $fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";
                  
                  if(!file_exists($fileName)) {
                         $out['status'] = 'error';
                         $out['message'] = 'File not found.';
                         exit(json_encode($out));
                  }
                  $data = array('name' => 'Foo', 'file' => '@'.$fileName);
                  
                  $cURL = curl_init("http://myapi/upload-images");
                  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt($cURL, CURLOPT_POST, 1);
                  curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
                  
                  $response = curl_exec($cURL);
                  $error = curl_error($cURL);
                  curl_close($cURL);
                  
                  die($response);
                  

                  有了这个,我没有错误,但在服务器中 $_POST 和 $_SERVER 数组是空的.

                  With this, I have no erros, but in server the $_POST and $_SERVER arrays is empty.

                  我尝试了其他方法,这次在发送之前创建了一个 Curl 文件:

                  I tried otherwise, this time creating a Curl file before send:

                  // Mime type of file 
                  $finfo = finfo_open(FILEINFO_MIME_TYPE);
                  $finfo = finfo_file($finfo, $fileName);
                  
                  $cFile = new CURLFile($fileName, $finfo, "file");
                  
                  //var_dump($cFile);
                  //CURLFile Object
                  //(
                  //   [name] => C:/.../test.pdf
                  //   [mime] => application/pdf
                  //   [postname] => file
                  // )
                  
                  $cURL = curl_init("http://myapi/upload-images");
                  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt($cURL, CURLOPT_POST, true);
                  curl_setopt($cURL, CURLOPT_POSTFIELDS, 
                  array(
                       'file' => $cFile
                  ));
                  
                  $response = curl_exec($cURL);
                  curl_close($cURL);
                  
                  die($response);
                  

                  同样的反应.$_FILES 为空.

                  Same response. $_FILES is empty.

                  推荐答案

                  终于找到问题的原因了.包含文件数据的数组必须有文件数据和文件名键.

                  Finally I found the reason of issue. The array with file data must have filedata and filename keys.

                  我们可以在带有完整路径的文件名之前传递@",但这已被弃用.

                  We can pass '@' before file name with full path but this is deprecated.

                  $data = array( "filedata" => '@'.$fileName, "filename" => basename($fileName));
                  

                  在这种情况下,我添加了一个 Curl 对象:

                  In this case I added a Curl object:

                  $finfo = finfo_open(FILEINFO_MIME_TYPE);
                  $finfo = finfo_file($finfo, $fileName);
                  
                  $cFile = new CURLFile($fileName, $finfo, basename($fileName));
                  
                  $data = array( "filedata" => $cFile, "filename" => $cFile->postname);
                  

                  完整代码为:

                  $fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";
                  $fileSize = filesize($fileName);
                  
                  if(!file_exists($fileName)) {
                      $out['status'] = 'error';
                      $out['message'] = 'File not found.';
                      exit(json_encode($out));
                  }
                  
                  $finfo = finfo_open(FILEINFO_MIME_TYPE);
                  $finfo = finfo_file($finfo, $fileName);
                  
                  $cFile = new CURLFile($fileName, $finfo, basename($fileName));
                  $data = array( "filedata" => $cFile, "filename" => $cFile->postname);
                  
                  $cURL = curl_init("http://myapi/upload-images")
                  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
                  
                  // This is not mandatory, but is a good practice.
                  curl_setopt($cURL, CURLOPT_HTTPHEADER,
                      array(
                          'Content-Type: multipart/form-data'
                      )
                  );
                  curl_setopt($cURL, CURLOPT_POST, true);
                  curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
                  curl_setopt($cURL, CURLOPT_INFILESIZE, $fileSize);
                  
                  $response = curl_exec($cURL);
                  curl_close($cURL);
                  
                  
                  die($response);
                  

                  这篇关于PHP 通过 cURL 发送本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  PHP Upload File Validation(PHP 上传文件验证)
                  PHP Error - Uploading a file(PHP 错误 - 上传文件)
                  How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                  php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                  How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                  change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
                    <tbody id='AvUEc'></tbody>
                    <i id='AvUEc'><tr id='AvUEc'><dt id='AvUEc'><q id='AvUEc'><span id='AvUEc'><b id='AvUEc'><form id='AvUEc'><ins id='AvUEc'></ins><ul id='AvUEc'></ul><sub id='AvUEc'></sub></form><legend id='AvUEc'></legend><bdo id='AvUEc'><pre id='AvUEc'><center id='AvUEc'></center></pre></bdo></b><th id='AvUEc'></th></span></q></dt></tr></i><div id='AvUEc'><tfoot id='AvUEc'></tfoot><dl id='AvUEc'><fieldset id='AvUEc'></fieldset></dl></div>
                      <bdo id='AvUEc'></bdo><ul id='AvUEc'></ul>

                      <legend id='AvUEc'><style id='AvUEc'><dir id='AvUEc'><q id='AvUEc'></q></dir></style></legend>

                      <small id='AvUEc'></small><noframes id='AvUEc'>

                      <tfoot id='AvUEc'></tfoot>