php 用PHP从ftp服务器下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7793170/
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-26 03:22:52 来源:igfitidea点击:
Download files from ftp server in PHP
提问by Sitansu
I am using the following format in php code to download files from ftp server.
我在 php 代码中使用以下格式从 ftp 服务器下载文件。
file_put_contents(
$filePath.$fileName, file_get_contents(
ftp://username:password@server_name/folder_name/xyz#123.csv.zip
)
);
I am getting an 550 error as the file name contains '#'. How to avoid the error. Also what is the best PHP class to do different operations on FTP ?
我收到 550 错误,因为文件名包含“#”。如何避免错误。另外,在 FTP 上执行不同操作的最佳 PHP 类是什么?
回答by user580950
Use this
用这个
<?php
// define some variables
$local_file = 'filename.jpg';
$server_file = 'filename.jpg';
$ftp_server="www.abc.com";
$ftp_user_name="username";
$ftp_user_pass="password";
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
回答by admin
true == (
$data = @
file_get_contents('ftp://username:password@server_name/folder_name/xyz#123.csv')
)
?
file_put_contents('xyz#123.csv', $data)
:
exit;
回答by run_time_rookie
Try:
尝试:
$output = exec("wget -N ftp://[email protected]/path/to directory/file 2>&1 |grep 'filename w/o extension'|grep -v ftp|grep -v =");
print $output <to check if file downloaded or not>