<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 发送本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <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>