Search code examples
phpftpcorruptionwinrar

File corrupted after downloading it with php (ftp)


I have downloaded a file using the code below. But when I try to open it with winrar to extract the file inside the gz folder I get the message that the file is currupted.

Here is my code:

$local_file = "wp-content/uploads/wpallimport/files/product-feed_toys.csv.gz";
$server_file = "product-feed_toys.csv.gz";

$ftp_username="hidden";
$ftp_userpass="hidden";


$ftp_server = "hidden";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to 
$ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);


if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)){
echo "Successfully written to $local_file. ";
}

else{
echo "Error downloading $server_file.";
}


ftp_close($ftp_conn);

Is my file corrupted during copying the file from server a to server b? Or does it have to be damaged in the first place?

I am downloading a file thats been used by alot of people, so thats why it'd be strange if it would be corrupted before my download. Thanks for helping!


Solution

  • Try switching the FTP mode to binary, i.e. replace this line:

    if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {

    with this:

    if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY)) {

    More information here