使用 curl 和 php 从 ftp 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1178425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:22:18  来源:igfitidea点击:

download a file from ftp using curl and php

phpcurl

提问by Ryan

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 和 php 从 ftp 服务器下载文件,但找不到任何帮助文档

$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

我可以获得文件列表,但仅此而已

回答by Tobias R

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.

我的猜测是您的 URL 指向一个目录,而不是一个文件。您需要向 CURLOPT_URL 提供文件的完整 URL。此外,如果您想下载文件,您可能希望将其保存在某处。

Working example:

工作示例:

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

回答by jimyi

  • Set CURLOPT_URL to the full path of the file.
  • Remove the CURLOPT_FTPLISTONLY line.
  • Add these lines before curl_exec:

    $file = fopen("filename_to_save_to", "w");
    curl_setopt($curl, CURLOPT_FILE, $file);
    
  • 将 CURLOPT_URL 设置为文件的完整路径。
  • 删除 CURLOPT_FTPLISTONLY 行。
  • 在 curl_exec 之前添加这些行:

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

回答by Yack

Community response works with minor adjustment :

社区响应工作稍作调整:

It appears that setting CURLOPT_FILE before setting CURLOPT_RETURNTRANSFER doesn't work, presumably because CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.

似乎在设置 CURLOPT_RETURNTRANSFER 之前设置 CURLOPT_FILE 不起作用,大概是因为 CURLOPT_FILE 取决于设置的 CURLOPT_RETURNTRANSFER。

Quoting joeterranova's comment on this page: http://php.net/manual/fr/function.curl-setopt.php

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

回答by ToughPal

See CURL help including how to connect to it via FTP here http://www.linuxformat.co.uk/wiki/index.php/PHP_-_The_Curl_library

在此处查看 CURL 帮助,包括如何通过 FTP 连接到它http://www.linuxformat.co.uk/wiki/index.php/PHP_-_The_Curl_library

NOTE: IF the file can be accessed from HTTP then it is better to just use the link EG: http://host.com/file.txtand then use file_get_contents or file functions.

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

You can then use http://uk.php.net/file_get_contentsor any other way to download the file to your computer. This option will be better than using FTP for download. You can always use FTP to upload as mentioned in the link above.

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

回答by leek

After trying all of these answers and having none of them work, this is what I finally got to work.

在尝试了所有这些答案并且没有一个起作用之后,这就是我最终开始工作的内容。

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

回答by Smile

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