I'm trying using this script to download a zip file from an FTP source (on localhost it works but not on the live server form OVH).
When running it on a live server I get instantly this:
So all the connection are good but it get stuck on downloading the file.
What could be the issue, or how could I get some error reports for this?
Some thinks that I have checked:
What else could it be?
// Connect to FTP server
// Use a correct ftp server
$ftp_server = "localhost";
// Use correct ftp username
$ftp_username="user";
// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass="user";
// Establishing ftp connection
$ftp_connection = ftp_connect($ftp_server, 21)
or die("Could not connect to $ftp_server");
if( $ftp_connection ) {
echo "successfully connected to the ftp server!";
// Logging in to established connection
// with ftp username password
$login = ftp_login($ftp_connection,
$ftp_username, $ftp_userpass);
if($login) {
// Checking whether logged in successfully
// or not
echo "<br>logged in successfully!";
// Name or path of the localfile to
// where the file to be downloaded
$local_file = "file.zip";
// Name or path of the server file to
// be downoaded
$server_file = "file.zip";
// Downloading the specified server file
if (ftp_get($ftp_connection, $local_file,
$server_file, FTP_BINARY)) {
echo "<br>Successfully downloaded from"
. " $server_file to $local_file.";
}
else {
echo "<br>Error while downloading from"
. " $server_file to $local_file.";
}
}
else {
echo "<br>login failed!";
}
// echo ftp_get_option($ftp_connection, 1);
// Closeing connection
if(ftp_close($ftp_connection)) {
echo "<br>Connection closed Successfully!";
}
}
?>
Most typical cause of problems with ftp_get
is that PHP defaults to the FTP active mode. And in 99% cases, one has to switch to the FTP passive mode, to make the transfer working. Use the ftp_pasv
function after ftp_login
:
ftp_pasv($connect, true) or die("Passive mode failed");
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV
command (what is quite common, if the server is behind firewall/NAT), you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
If none of this helps, test the transfer using any commandline/GUI FTP client running on the same machine, where your PHP code runs (i.e. typically on your webserver). If it does not work either, you have a network problem, which can hardly be fixed in your PHP code. You need to fix the network. Test transfers from other machines too, so check if the problem is on the FTP server or on your webserver (or theirs networks).