使用Curl使用PHP从FTP下载文件 [英] Download a file from an FTP using Curl with PHP
问题描述
我尝试使用Curl从FTP服务器下载文件。我可以将URL粘贴到我的浏览器,它下载正常。问题是,使用Curl和PHP,我相信FTP服务器正在做一个重定向和Curl不会跟随。我收到错误消息服务器拒绝您更改到给定的目录。
I'm trying to download a file from an FTP server using Curl. I can paste the URL into my browser and it downloads fine. The problem is that with Curl and PHP, I believe the FTP server is doing a redirect and Curl won't follow. I get the error message, "Server denied you to change to the given directory".
编辑:我有%2f的代码,并删除它。
I had %2f in the code and deleted it. That was from a prior test.
我的代码是:
$curl = curl_init();
$file = fopen("temp.zip", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://idx.living.net/idx_fl_ftp_down/idx_naples_dn/naples_data.zip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_SSL);
curl_setopt($curl, CURLOPT_USERPWD, "myusername:mypassword");
curl_exec($curl);
echo curl_error($curl);
任何方式强制Curl的行为方式与浏览器下载文件时相同?
Any way to force Curl to behave the way the browser does when downloading the file?
推荐答案
我知道这不会回答你的问题的方式应该是(使用curl)。
但是这里有一个非curl代码用于使用php在ftp服务器上下载文件。
I know this dont answer your question the way it should be ( using curl ). But here's a non curl code to download a file on a ftp server with php.
$local_file = 'output.rar';
$server_file = '/somedir/1/bar/foo/somearchive.rar';
$ftp_user_name='some_login';
$ftp_user_pass='Password';
$ftp_server='HOST';
// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
/* uncomment if you need to change directories
if (ftp_chdir($conn_id, "<directory>")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
*/
// 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);
注意:
- ftp_ssl_connect()不适用于sFTP。要使用PHP的sFTP,请参阅ssh2_sftp()。
- FTP服务器地址参数不应包含任何尾部斜线,且不能带有前缀ftp://。
这篇关于使用Curl使用PHP从FTP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!