views:

200

answers:

2

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!

+2  A: 

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

Ignacio Vazquez-Abrams
+1 as I have fallen foul of this myself.
Xetius
"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."
plod
That was my first thought, actually. That's why I tried uploading it to the server first as a zipped file, then unzipping it once it was uploaded. Wouldn't that have avoided any corruption issues for the actual exe file on upload? (Hah. plod beat me to it)
Kerri
+2  A: 

"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.

Bart van Heukelom
Can you tell me more about what you mean by "how the exe is started after it has been downloaded"? Do I have any control over this from the website? Thanks!
Kerri
After the exe is downloaded it must be started. By "how" I mean does the browser start it, does the user click it in explorer, etc. You have no control over that.That's a weird place for a problem though, so are you 10000% sure the files are binary identical?
Bart van Heukelom
The file is meant to be double clicked by the user to start it. It's an install file, and the install process should begin when it's clicked. Instead, a blank DOS window is coming up.
Kerri
Have you used a binary diff application to make sure they are **exactly** the same? If so, try moving the application(from where you upload) to where you download it to test that it isn't some weird directory issue
Earlz
Earlz. Yes, I used the KDiff3 app to compare the two files and it found no differences, not a one. I'm clear on your suggestion, though. The file is uploaded via ftp to the same directory that it is downloaded from.
Kerri
Earlz meant that you should try running the downloaded file from the same directory that the originally uploaded one is in (on your computer, not the server). If the files are binary equal, you can completely exclude the server and site from the list of where to search for the cause.
Bart van Heukelom