问题
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".
EDIT: I had %2f in the code and deleted it. That was from a prior test.
My code is:
$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);
Any way to force Curl to behave the way the browser does when downloading the file?
回答1:
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);
Notes:
- ftp_ssl_connect() is not intended for use with sFTP. To use sFTP with PHP, please see ssh2_sftp().
- The FTP server address parameter shouldn't have any trailing slashes and shouldn't be prefixed with ftp://.
来源:https://stackoverflow.com/questions/10373522/download-a-file-from-an-ftp-using-curl-with-php