2012-06-12 97 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服务器(错误)日志说什么?您是否通过了身份验证(并且是正确的用户?),远程目录是否存在,是否可以通过已验证的用户进行写入和读取?..? –