PHP cURL错误URL格式不正确

时间:2012-06-12 15:47:36

标签: php curl

我正在尝试将一个简单的cURL文件从一个服务器上传到另一个服务器。问题是我从cUrl错误代码中得到错误#3: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:password@199.38.215.1xx/');
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。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的远程网址需要包含目标文件的路径和名称,如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); 
?>