使用curl和php从ftp下载一个文件

问题描述:

我尝试使用curl和php从ftp服务器下载文件,但我找不到任何帮助文档

I'm trying to download a file from an ftp server using curl and php but I can't find any documentation to help

$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);

我可以得到一个文件列表,但是关于它的

i can get a list of files but thats about it

我的猜测是你的URL指向一个目录,而不是一个文件。您需要将CURLOPT_URL的完整网址提供给该文件。

My guess is that your URL is pointing towards a directory, not a file. You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere.

工作范例:

$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);