Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.exe File becomes corrupted when downloaded from server

Firstly: I'm a lowly web designer who knows just enough PHP to be dangerous and just enough about server administration to be, well, nothing. I probably won't understand you unless you're very clear!

The setup: I've set up a website where the client uploads files to a specific directory, and those files are made available, through php, for download by users. The files are generally executable files over 50MB. The client does not want them zipped, as they feel their users aren't savvy enough to unzip them. I'm using the php below to force a download dialogue box and hide the directory where the files are located.

It's Linux server, if that makes a difference.

The problem: There is a certain file that becomes corrupt after the user tries to download it. It is an executable file, but when it's clicked on, a blank DOS window opens up. The original file, prior to download opens perfectly. There are several other similar files that go through the same exact download procedure, and all of those work just fine.

Things I've tried: I've tried uploading the file zipped, then unzipping it on the server to make sure it wasn't becoming corrupt during upload, and no luck.
I've also compared the binary code of the original file to the downloaded file that doesn't work, and they're exactly the same (so the php isn't accidentally inserting anything extra into the file).

Could it be an issue with the headers in my downloadFile function? I really am not sure how to troubleshoot this one…

This is the download php, if it's relevant ($filenamereplace is defined elsewhere):

downloadFile("../DIRECTORY/files/$filenamereplace","$filenamereplace");

function downloadFile($file,$filename){    
    if(file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        @ flush();
        readfile($file);
        exit;
    }
}

ETA Additonal Info: - Tests for working/non-working files have been done on the same machine - If it makes any difference, the original file has a custom icon. After download, the file has a generic blank document icon.

Additonal Info: I THINK THIS ONE'S IMPORTANT! I just tried downloading the file directly (to bypass the download link that triggers the download function above). If I download the file by just going to its url and downloading it that way, the downloaded file WORKS. So I'm thinking it must have something to do with the download function. But what??

3/17 MAJOR CORRECTION —AND RESOLVED— So I woke up this morning and it dawned on me that maybe I was comparing the files wrong. (I had re-saved them as binary text, and then compared them. I didn't realize the comparison program would take and compare actual exe files). This morning I tried comparing the actual exe files and there is a difference. There was one line of php code that was being injected into the first line of the file. I adjusted the php, and the problem was fixed. (It was from the if/else statement that defined teh $filenamereplace variable in the code I'd cited). Thanks again for all your help, and sorry for misleading you in insisting that the files' contents were identical!

like image 243
Kerri Avatar asked Mar 16 '10 21:03

Kerri


People also ask

Why does my downloaded file corrupt?

The corruption of data may happen because of the following reasons: 1. Such problems may occur because of conflicts between IDM and other applications. For example it can be some FireWall/Antivirus/Internet security application that controls network downloading processes and writing data to disks.

How do I fix a corrupt exe file?

File extension fixer is a free tool intended to fix executable file associations and run programs even when those extensions are damaged. It also includes several fixes to common issues caused by modern malware to windows registry. Version .com available to be used if the .exe file association has been corrupted.

Why can I not download an exe file?

If executable file downloads fail from certain sites but you are able to download exe files from other sites, check in "Internet Options -> Security - > Restricted sites -> Sites" to make sure that the problem site is not in the "Restricted sites" list, as the security level for that zone is High by default.


2 Answers

"I've also compared the binary code of the original file to the downloaded file that doesn't work, and their exactly the same (so the php isn't accidentally inserting anything extra into the file)."

If that's really true, then the problem must be in how the exe is started after it has been downloaded. It should certainly not be a problem with your PHP code.

like image 88
Bart van Heukelom Avatar answered Oct 22 '22 11:10

Bart van Heukelom


Perhaps they were corrupted on upload. This can happen if you transfer them via FTP in ASCII mode instead of BINARY.

like image 32
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 09:10

Ignacio Vazquez-Abrams