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

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

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

      1. 使用 API PUT 请求上传文件

        时间:2023-10-12
          <tbody id='mLsG9'></tbody>
        <i id='mLsG9'><tr id='mLsG9'><dt id='mLsG9'><q id='mLsG9'><span id='mLsG9'><b id='mLsG9'><form id='mLsG9'><ins id='mLsG9'></ins><ul id='mLsG9'></ul><sub id='mLsG9'></sub></form><legend id='mLsG9'></legend><bdo id='mLsG9'><pre id='mLsG9'><center id='mLsG9'></center></pre></bdo></b><th id='mLsG9'></th></span></q></dt></tr></i><div id='mLsG9'><tfoot id='mLsG9'></tfoot><dl id='mLsG9'><fieldset id='mLsG9'></fieldset></dl></div>

      2. <small id='mLsG9'></small><noframes id='mLsG9'>

        • <bdo id='mLsG9'></bdo><ul id='mLsG9'></ul>
          <tfoot id='mLsG9'></tfoot>

                  <legend id='mLsG9'><style id='mLsG9'><dir id='mLsG9'><q id='mLsG9'></q></dir></style></legend>
                  本文介绍了使用 API PUT 请求上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在用 PHP 构建 API.其中一种方法是 place.new(PUT 请求).它需要几个字符串字段,还需要一个图像.但是我无法让它工作.使用 POST 请求很容易,但我不确定如何使用 PUT 来执行此操作以及如何在服务器上获取数据.

                  I'm building an API in PHP. One of the methods is place.new (PUT request). It expects several string fields, and it also expects an image. However I can't get it working. With a POST request it was easy, but I'm not sure how to do it with a PUT and how to get the data on the server.

                  感谢您的帮助!

                  $curl = curl_init();
                  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
                  curl_setopt($curl, CURLOPT_HEADER, false);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_URL, $this->url);
                  
                  curl_setopt($curl, CURLOPT_PUT, 1);
                  curl_setopt($curl, CURLOPT_INFILE, $image);
                  curl_setopt($curl, CURLOPT_INFILESIZE, filesize($image));
                  
                  $this->result = curl_exec($curl);
                  curl_close($curl); 
                  

                  服务器代码

                  if ( $im_s = file_get_contents('php://input') )
                  {
                      $image = imagecreatefromstring($im_s);
                  
                      if ( $image != '' )
                      {
                          $filename = sha1($title.rand(11111, 99999)).'.jpg';
                          $photo_url = $temp_dir . $filename;
                          imagejpeg($image, $photo_url);
                  
                          // upload image
                          ...
                      }
                  }
                  

                  解决方案

                  发送

                  // Correct: /Users/john/Sites/....
                  // Incorrect: http://localhost/...
                  $image = fopen($file_on_dir_not_url, "rb");
                  
                  $curl = curl_init();
                  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
                  curl_setopt($curl, CURLOPT_HEADER, false);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_URL, $url);
                  
                  curl_setopt($curl, CURLOPT_PUT, 1);
                  curl_setopt($curl, CURLOPT_INFILE, $image);
                  curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_on_dir_not_url));
                  
                  $result = curl_exec($curl);
                  curl_close($curl); 
                  

                  接收

                  /* Added to clarify, per comments */
                  $putdata = fopen("php://input", "r");
                  
                  /* Open a file for writing */
                  $fp = fopen($photo_url, "w");
                  
                  /* Read the data 1 KB at a time
                      and write to the file */
                  while ($data = fread($putdata, 1024))
                  {
                      fwrite($fp, $data);
                  }
                  
                  /* Close the streams */
                  fclose($fp);
                  fclose($putdata);
                  

                  推荐答案

                  您是否阅读了 http://php.net/manual/en/features.file-upload.put-method.php ?Script PUT/put.php 都设置好了吗?

                  Did you read http://php.net/manual/en/features.file-upload.put-method.php ? Script PUT /put.php all set up?

                  另外,什么是 $image -- 它需要是一个文件处理程序,而不是一个文件名.

                  Also, what is $image -- it needs to be a file handler, not a file name.

                  附言.使用 file_get_contents 将尝试将服务器上的任何内容加载到内存中.不是个好主意.请参阅链接的手册页.

                  Ps. Using file_get_contents will try to load whatever is PUT on the server into memory. Not a good idea. See the linked manual page.

                  这篇关于使用 API PUT 请求上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:请求源 下一篇:Symfony 2 使用 POST 重定向

                  相关文章

                    • <bdo id='3SUHs'></bdo><ul id='3SUHs'></ul>
                  1. <tfoot id='3SUHs'></tfoot>

                    <small id='3SUHs'></small><noframes id='3SUHs'>

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

                      <legend id='3SUHs'><style id='3SUHs'><dir id='3SUHs'><q id='3SUHs'></q></dir></style></legend>