2009-07-24 82 views
22

我试图使用卷曲和PHP下载从FTP服务器的文件,但我无法找到任何文件,以帮助下载使用卷曲和PHP

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,"ftp://$_FTP[server]"); 
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1); 
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

$result = curl_exec ($curl); 

我可以得到一个列表从FTP文件的文件,但多数民众赞成在它

+1

你试图给它的完整URL,文件和去除FTPLISTONLY选项? – cOle2 2009-07-24 15:37:11

回答

4
  • 设置CURLOPT_URL文件的完整路径。
  • 删除CURLOPT_FTPLISTONLY行。
  • curl_exec之前添加这些行:

    $file = fopen("filename_to_save_to", "w"); 
    curl_setopt($curl, CURLOPT_FILE, $file); 
    
20

我的猜测是,您的网址是朝着一个目录,而不是文件指向。您需要将CURLOPT_URL提供给文件的完整URL。另外如果你想下载一个文件,你可能想把它保存在某个地方。

工作例如:

$curl = curl_init(); 
$file = fopen("ls-lR.gz", 'w'); 
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_FILE, $file); #output 
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]"); 
curl_exec($curl); 
curl_close($curl); 
fclose($file); 
1

见,包括如何通过FTP连接到它这里卷曲帮助http://www.linuxformat.co.uk/wiki/index.php/PHP_-_The_Curl_library

注意:如果文件可以从HTTP访问那么最好是只使用链接EG:http://host.com/file.txt然后使用file_get_contents或文件功能。

然后,您可以使用http://uk.php.net/file_get_contents或任何其他方式将文件下载到您的计算机。这个选项比使用FTP下载更好。您可以随时使用FTP上传,如上面链接中所述。

0

经过尝试所有这些答案,并没有他们的工作,这是我终于工作。

$curl = curl_init(); 
$fh = fopen("FILENAME.txt", 'w'); 
curl_setopt($curl, CURLOPT_URL, "ftp://{$serverInfo['username']}:{$servererInfo['password']}@{$serverInfo['server']}/{$serverInfo['file']}"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl); 
fwrite($fh, $result); 
fclose($fh); 
curl_close($curl); 
1
$ftp_location = put your ftp adress here; 
$location_login = put your login here; 
$location_pwd = put your password here; 
$conn_id = ftp_connect("$ftp_location"); 
$login_result = ftp_login($conn_id, $location_login, $location_pwd); 

if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!"; 
    exit; 
} else { 
    echo "Connected"; 
} 

// get the file 
// change products.csv to the file you want 
$local = fopen("products.csv","w"); 
$result = ftp_fget($conn_id, $local,"products.csv", FTP_BINARY); 
fwrite($local, $result); fclose($local); 

// check upload status 
if (!$result) { 
    echo "FTP download has failed!"; 
} else { 
    echo "Downloaded ";  
} 

// close the FTP stream 
ftp_close($conn_id); 
4

社区响应工作与微调:

看来,设置CURLOPT_RETURNTRANSFER 前设置CURLOPT_FILE 因为 CURLOPT_FILE取决于 CURLOPT_RETURNTRANSFER被设置不起作用,大概。

引用此页面上joeterranova的评论:http://php.net/manual/fr/function.curl-setopt.php