2012-06-12 95 views
2

我試圖做一個簡單的cURL文件從一臺服務器上傳到另一臺服務器。問題是我從cUrl錯誤代碼中獲得錯誤#3:URL格式不正確。PHP cURL錯誤URL格式不正確

我已將網址複製到我的瀏覽器並登錄到ftp站點,沒有任何問題。我還驗證了正確的格式,並在網站和本網站上搜索了一個沒有任何成功的答案。

下面的代碼:

$ch = curl_init(); 

$localfile = '/home/httpd/vhosts/homeserver.com/httpdocs/admin.php'; 
echo $localfile; //This reads back to proper path to the file 
$fp = fopen($localfile, 'r'); 
curl_setopt($ch, CURLOPT_URL, 'ftp://username:[email protected]/'); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); 
curl_exec ($ch); 
$error_no = curl_errno($ch); 
curl_close ($ch); 
if ($error_no == 0) { 
    $error = 'File uploaded succesfully.'; 
} else { 
    $error = 'Upload error:'.$error_no ;//Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html'; 
} 
echo $error; 

我也試過這樣:

curl_setopt($ch, CURLOPT_URL, 'ftp://199.38.215.1xx/'); 
curl_setopt($ch, CURLOPT_USERPWD, 'username:password'); 

我仍然得到錯誤#3。

任何想法?

+2

PHP內置的FTP功能可能比捲曲更好地工作。 –

回答

0

遠程URL需要包含目標文件的路徑和名稱,如本example

<?php 
// FTP upload to a remote site Written by Daniel Stenberg 
// original found at http://curl.haxx.se/libcurl/php/examples/ftpupload.html 
// 
// A simple PHP/CURL FTP upload to a remote site 
// 

$localfile = "me-and-my-dog.jpg"; 
$ftpserver = "ftp.mysite.com"; 
$ftppath = "/path/to"; 
$ftpuser = "myname"; 
$ftppass = "mypass"; 

$remoteurl = "ftp://${ftpuser}:${ftppasswd}@${ftpserver}${ftppath}/${localfile}"; 

$ch = curl_init(); 

$fp = fopen($localfile, "rb"); 

// we upload a JPEG image 
curl_setopt($ch, CURLOPT_URL, $remoteurl); 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 

// set size of the image, which isn't _mandatory_ but helps libcurl to do 
// extra error checking on the upload. 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); 

$error = curl_exec($ch); 

// check $error here to see if it did fine or not! 

curl_close($ch); 
?> 
+0

感謝這個例子 - 我有點接近。我修改了這個例子來回顯錯誤代碼,並得到錯誤9:「我們被拒絕訪問URL中給出的資源。對於FTP,在嘗試更改到遠程目錄時發生這種情況。」我在同一臺服務器上嘗試了一個不同的ftp站點,並確保權限是正確的(我可以無故障地ftp),所以我不知道下一步要轉向哪裏。 –

+0

你的ftp服務器(錯誤)日誌說什麼?您是否通過了身份驗證(並且是正確的用戶?),遠程目錄是否存在,是否可以通過已驗證的用戶進行寫入和讀取?..? –