使用 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 提供文件的完整 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);