2017-07-05 200 views
1

我正在使用此腳本通過curl函數和ftp連接來上傳myfile。如何使用ftp連接通過PHP cURL上傳文件?

在本地它工作正常,但在我的服務器文件出現上傳,但它沒有文件大小。

有什麼不對?謝謝。

$ch = curl_init(); 
$localfile = (dirname(__FILE__).'/asset/myfile.zip'); 
$fp = fopen($localfile, 'r'); 
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:[email protected]$server/".'myfile.zip'); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); 
curl_exec ($ch); 
curl_close ($ch); 

,以及如何我可以上傳多個文件中的腳本,如:

$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip'); 
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip'); 
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip'); 

回答

2

上傳文件中捲曲你可以使用curl_file_create。嘗試下面的一個:

$localfile = (dirname(__FILE__).'/asset/myfile.zip'); 
$curl_file = curl_file_create($localfile,'zip'); 
$params = ['file' => $curl_file]; 

$ch = curl_init(); 
$localfile = (dirname(__FILE__).'/asset/myfile.zip'); 
$fp = fopen($localfile, 'r'); 
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:[email protected]$server/".'myfile.zip'); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
//curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); 
curl_exec ($ch); 
curl_close ($ch);