問題描述
使用 html 表單,我們可以使用 enctype="multipart/form-data"
將文件從客戶端上傳到服務器,輸入 type="file"
等等在.
with html forms we can upload a file from a client to a server with enctype="multipart/form-data"
, input type="file"
and so on.
有沒有辦法讓一個文件已經在服務器上,然后以同樣的方式將它傳輸到另一臺服務器?
Is there a way to have a file already ON the server and transfer it to another server the same way?
感謝提示.
//哇!這是我見過最快的問答頁面!!
// WoW! This is the fastest question answering page i have ever seen!!
推薦答案
當瀏覽器將文件上傳到服務器時,它會發送一個包含文件內容的 HTTP POST 請求.
When the browser is uploading a file to the server, it sends an HTTP POST request, that contains the file's content.
你必須復制它.
對于 PHP,最簡單的(或者至少是最常用的) 解決方案可能是使用 curl
.
With PHP, the simplest (or, at least, most used) solution is probably to work with curl
.
如果您查看可以使用 curl_setopt設置的選項列表代碼>
,你會看到這個:CURLOPT_POSTFIELDS
(引用):
If you take a look at the list of options you can set with curl_setopt
, you'll see this one : CURLOPT_POSTFIELDS
(quoting) :
要在 HTTPPOST"中發布的完整數據手術.
要發布文件,在文件名前面加上@ 并使用完整路徑.
這可以是作為 urlencoded 字符串傳遞,如'para1=val1¶2=val2&...' 或作為以字段名稱為鍵的數組和字段數據作為值.
如果值是一個數組,Content-Type 標頭將設置為 multipart/form-data.
The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.
This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value.
If value is an array, the Content-Type header will be set to multipart/form-data.
未經測試,但我認為這樣的事情應該可以解決問題 - 或者至少
幫助您入門:
Not tested,but I suppose that something like this should do the trick -- or at least
help you get started :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => '@/..../file.jpg',
// you'll have to change the name, here, I suppose
// some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);
基本上,你:
- 正在使用 curl
- 必須設置目標網址
- 表示你希望
curl_exec
返回結果,而不是輸出它 - 正在使用
POST
,而不是GET
- 正在發布一些數據,包括一個文件 -- 請注意文件路徑前的
@
.
- are using curl
- have to set the destination URL
- indicate you want
curl_exec
to return the result, and not output it - are using
POST
, and notGET
- are posting some data, including a file -- note the
@
before the file's path.
這篇關于從服務器到服務器的http傳輸文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!