問題描述
我意識到我可以很容易地使用 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模板網!