2010-04-05 182 views
7

我們可以使用enctype =「multipart/form-data」,輸入類型=「文件」等將客戶端上傳到服務器的文件。使用html格式從服務器到服務器的http傳輸文件

有沒有辦法讓文件已經在服務器上,並以相同的方式將其傳輸到另一臺服務器?

感謝提示。

//哇!這是我見過的最快的問題解答頁面!例如:

回答

10

當瀏覽器上傳文件到服務器時,它發送一個HTTP POST請求,其中包含文件的內容。

您將有te複製。


使用PHP,最簡單的(或者,至少,最常用的)解決辦法可能是與curl工作。

如果你看一看選項列表中你可以設置curl_setopt,你會看到這樣一句:CURLOPT_POSTFIELDS(引用)

完整數據的HTTP發佈「POST」 操作。
要發佈文件, 需要在文件名前添加@並使用 完整路徑
這可以被 作爲urlencoded的字符串等 「PARA1 = VAL1 & PARA2 = val2的& ...」或作爲 陣列與現場名作爲值作爲密鑰和 場數據通過。
如果值爲 一個數組,則Content-Type頭將 設置爲multipart/form-data。


沒有測試,但我想這樣的事情應該做的伎倆 - 或者,至少,幫助您開始:

$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_exec返回結果,而不是將它輸出
  • 使用POST,而不是GET
  • 張貼一些數據,包括文件 - 注意文件的路徑前@
+0

我認爲這是行得通的,我會試試這個!謝謝:-) (嘿,你有相同的姓氏,那麼我所以你的回答必須是正確的^^) – qualle 2010-04-05 11:49:28

+0

不客氣:-)玩得開心! *(很多人用我們的名字,我看到^^)* – 2010-04-05 11:50:59

+0

+1,我沒有意識到它和使用PHP的'@'符號一樣簡單:-) – 2010-04-05 11:58:00

0

Ex。如果服務器A上有一個名爲mypicture.gif的文件,並且想將它發送給服務器B,則可以使用CURL。

  1. 服務器A以字符串形式讀取內容。
  2. 後的捲曲度到服務器B
  3. 服務器B讀取字符串並將其存儲爲一個名爲mypictyre-copy.gif字符串

http://php.net/manual/en/book.curl.php

一些示例PHP代碼:

<?php 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_VERBOSE, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); 
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL); 
    curl_setopt($ch, CURLOPT_POST, true); 
    // same as <input type="file" name="file_box"> 
    $post = array(
     "file_box"=>"@/path/to/myfile.jpg", 
    ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
?> 
0

如果服務器都在您的控制之下,FTP可能是比HTTP更好的選擇。

+0

不幸的是,不是 – qualle 2010-04-05 11:46:10

+0

FTP並不是一個好的選擇,因爲遍歷防火牆和NAT網關很麻煩。堅持使用HTTP,甚至更好地使用HTTPS。先進的解決方案也考慮到webdav,但永遠不要FTP! – Raven 2010-04-05 11:48:40

+0

如果兩臺服務器都在asker的控制之下(就像我在答案中所說的那樣),我不明白防火牆是怎麼樣的問題? – 2010-04-05 12:10:41

3

你可以用同樣的方法做到這一點。就在這時,您首先收到文件的服務器就是客戶端,而第二臺服務器就是您的服務器。 嘗試使用這些:

對於第二個服務器上的網頁:

<form> 
     <input type="text" name="var1" /> 
     <input type="text" name="var2" /> 
     <input type="file" name="inputname" /> 
     <input type="submit" /> 
    </form> 

而作爲一個腳本發送的第一個服務器上的文件:

<?php 
function PostToHost($host, $port, $path, $postdata, $filedata) { 
    $data = ""; 
    $boundary = "---------------------".substr(md5(rand(0,32000)),0,10); 
    $fp = fsockopen($host, $port); 

    fputs($fp, "POST $path HTTP/1.0\n"); 
    fputs($fp, "Host: $host\n"); 
    fputs($fp, "Content-type: multipart/form-data; boundary=".$boundary."\n"); 

    // Ab dieser Stelle sammeln wir erstmal alle Daten in einem String 
    // Sammeln der POST Daten 
    foreach($postdata as $key => $val){ 
     $data .= "--$boundary\n"; 
     $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; 
    } 
    $data .= "--$boundary\n"; 

    // Sammeln der FILE Daten 
    $data .= "Content-Disposition: form-data; name=\"{$filedata[0]}\"; filename=\"{$filedata[1]}\"\n"; 
    $data .= "Content-Type: image/jpeg\n"; 
    $data .= "Content-Transfer-Encoding: binary\n\n"; 
    $data .= $filedata[2]."\n"; 
    $data .= "--$boundary--\n"; 

    // Senden aller Informationen 
    fputs($fp, "Content-length: ".strlen($data)."\n\n"); 
    fputs($fp, $data); 

    // Auslesen der Antwort 
    while(!feof($fp)) { 
     $res .= fread($fp, 1); 
    } 
    fclose($fp); 

    return $res; 
} 

$postdata = array('var1'=>'test', 'var2'=>'test'); 
$data = file_get_contents('Signatur.jpg'); 
$filedata = array('inputname', 'filename.jpg', $data); 

echo PostToHost ("localhost", 80, "/test3.php", $postdata, $filedata); 
?> 

兩個腳本都需要從這裏:http://www.coder-wiki.de/HowTos/PHP-POST-Request-Datei

相關問題