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

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

    2. <legend id='FPkKR'><style id='FPkKR'><dir id='FPkKR'><q id='FPkKR'></q></dir></style></legend>

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

      1. <tfoot id='FPkKR'></tfoot>

        使用 file_get_contents 上传文件

        时间:2023-05-20
        <legend id='1SNEB'><style id='1SNEB'><dir id='1SNEB'><q id='1SNEB'></q></dir></style></legend>

            <bdo id='1SNEB'></bdo><ul id='1SNEB'></ul>

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

                  本文介绍了使用 file_get_contents 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我意识到我可以很容易地使用 CURL 做到这一点,但我想知道是否可以使用 file_get_contents() 和 http 流上下文将文件上传到远程 Web 服务器,以及如果是这样,如何?

                  I realise I can do this with CURL very easily, but I was wondering if it was possible to use file_get_contents() with the http stream context to upload a file to a remote web server, and if so, how?

                  推荐答案

                  首先,multipart Content-Type 的第一条规则是定义一个边界用作每个部分之间的分隔符(因为顾名思义,它可以有多个部分).边界可以是内容正文中未包含的任何字符串.我通常会使用时间戳:

                  First of all, the first rule of multipart Content-Type is to define a boundary that will be used as a delimiter between each part (because as the name says, it can have multiple parts). The boundary can be any string that is not contained in the content body. I will usually use a timestamp:

                  define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
                  

                  一旦定义了边界,就必须将它与 Content-Type 标头一起发送,以告诉网络服务器期望的分隔符:

                  Once your boundary is defined, you must send it with the Content-Type header to tell the webserver what delimiter to expect:

                  $header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
                  

                  完成后,您必须构建与 HTTP 规范和您发送的标头相匹配的适当内容正文.如您所知,从表单发布文件时,您通常会有一个表单字段名称.我们将定义它:

                  Once that is done, you must build a proper content body that matches the HTTP specification and the header you sent. As you know, when POSTing a file from a form, you will usually have a form field name. We'll define it:

                  // equivalent to <input type="file" name="uploaded_file"/>
                  define('FORM_FIELD', 'uploaded_file'); 
                  

                  然后我们构建内容主体:

                  Then we build the content body:

                  $filename = "/path/to/uploaded/file.zip";
                  $file_contents = file_get_contents($filename);    
                  
                  $content =  "--".MULTIPART_BOUNDARY."
                  ".
                              "Content-Disposition: form-data; name="".FORM_FIELD.""; filename="".basename($filename).""
                  ".
                              "Content-Type: application/zip
                  
                  ".
                              $file_contents."
                  ";
                  
                  // add some POST fields to the request too: $_POST['foo'] = 'bar'
                  $content .= "--".MULTIPART_BOUNDARY."
                  ".
                              "Content-Disposition: form-data; name="foo"
                  
                  ".
                              "bar
                  ";
                  
                  // signal end of request (note the trailing "--")
                  $content .= "--".MULTIPART_BOUNDARY."--
                  ";
                  

                  如您所见,我们正在发送带有 form-data 配置的 Content-Disposition 标头以及 name 参数(表单字段名称)和 filename 参数(原始文件名).如果您想正确填充 $_FILES[]['type'] 东西,那么发送带有正确 MIME 类型的 Content-Type 标头也很重要.

                  As you can see, we're sending the Content-Disposition header with the form-data disposition, along with the name parameter (the form field name) and the filename parameter (the original filename). It is also important to send the Content-Type header with the proper MIME type, if you want to correctly populate the $_FILES[]['type'] thingy.

                  如果您有多个文件要上传,您只需使用 $content 位重复该过程,当然,每个文件都有不同的 FORM_FIELD.

                  If you had multiple files to upload, you just repeat the process with the $content bit, with of course, a different FORM_FIELD for each file.

                  现在,构建上下文:

                  $context = stream_context_create(array(
                      'http' => array(
                            'method' => 'POST',
                            'header' => $header,
                            'content' => $content,
                      )
                  ));
                  

                  并执行:

                  file_get_contents('http://url/to/upload/handler', false, $context);
                  

                  注意:在发送二进制文件之前无需对其进行编码.HTTP 可以很好地处理二进制文件.

                  NOTE: There is no need to encode your binary file before sending it. HTTP can handle binary just fine.

                  这篇关于使用 file_get_contents 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP - 上传 utf-8 文件名 下一篇:从Android设备上传php服务器中的文件

                  相关文章

                  最新文章

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

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

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