2013-05-31 110 views
-2

我需要創建一個腳本來讀取文件併發布數據,以便它可以在另一臺機器上接收。發送/接收文件PHP腳本

有沒有人有任何想法,我可以從哪裏開始呢? PHP.Net上唯一看起來類似的東西是使用dir("/etc/php5

回答

1

使用PHP,您可以使用libcurl將文件上傳到遠程服務器。你不需要做任何特殊的事情來閱讀文件,只需要給出捲曲的路徑。下面是發送文件的一個例子:

$target_url = 'http://127.0.0.1/accept.php'; 
//This needs to be the full path to the file you want to send. 
$file_name_with_full_path = realpath('./sample.jpeg'); 
/* curl will accept an array here too. 
* Many examples I found showed a url-encoded string instead. 
* Take note that the 'key' in the array will be the key that shows up in the 
* $_FILES array of the accept script. and the at sign '@' is required before the 
* file name. 
*/ 
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$result=curl_exec ($ch); 
curl_close ($ch); 
echo $result; 

此代碼是從Derak,看到http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/的所有細節。

請注意,您根本不需要PHP,you can simply use curl from the command line或使用shell腳本。

+0

您的鏈接不起作用。網關錯誤。你可以在你的答案中發佈細節嗎? – gibberish

+0

鏈接對我來說很好,但我更新了更多細節的答案。 – nullability

+0

鏈接現在也爲我工作,但感謝您張貼代碼。如果該網站再次出現問題,最好給未來的讀者。 – gibberish

0

使用XML PHP函數將數據解析爲轉儲文件,然後在另一臺機器上用不同的函數讀取它。