Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serving exe files

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I'm using the following code to serve up exe files as downloads on a site:
Code:
$filename = 'Update123.exe';
$mimetype = 'application/octet-stream';
    
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
  $mimetype = 'application/x-download';
}
header('content-disposition: attachment; filename=' . $filename);
header('content-type: ' . $mimetype);
header('content-length: ' . filesize($downloads_path . $filename));
readfile($downloads_path . $filename);
The exe downloads ok (i.e. the file that is downloaded matches the filesize of the file on the server). The exe is actually a WinZip Self-Extractor executable file. When I run it, it shows a black console screen for about 0.5 second then does nothing.

If I type the URL of the file in the browser and download the file directly, the file is represented by a WinZip icon once downloaded and, when clicked, displays a WinZip Self-Extractor dialog as expected.

Does anyone know what is causing this discrepancy? I presume I'm doing something incorrectly when sending the headers?!

Your advice would be much appreciated!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
try

Code:
header('Content-Disposition: attachment; filename="' . $filename.'"');
header('Content-Type: ' . $mimetype);
header('Content-Length: ' . (string) filesize($downloads_path . $filename));
header("Content-Transfer-Encoding: binary");
readfile($downloads_path . $filename);
 
By opening up the downloaded exe in WordPad, I noticed that the first line contained an error message relating to the filesize function! Basically, I was passing an absolute path to the filesize function when it only accepts relative paths - problem solved.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Just because I'm interested... did you change to use jpadie's suggestion, or merely change the path for the filesize function? I'm also keen to know what browser/OS combinations you have verified against - if you have that info at hand!

I don't serve .exe files at all... but when I have the need to know, it's always nice to have a reference [smile]

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Jeff,

Here's the code I went with in the end, incorporating jpadie's suggestions:
Code:
ob_start();
$fileExt = end(explode(".", $filename));
switch($fileExt) {
  case "pdf": $mimetype = "application/pdf"; break;
  case "exe": $mimetype = "application/octet-stream"; break;
  case "zip": $mimetype = "application/zip"; break;
  case "doc": $mimetype = "application/msword"; break;
  case "xls": $mimetype = "application/vnd.ms-excel"; break;
  case "ppt": $mimetype = "application/vnd.ms-powerpoint"; break;
  default:    $mimetype = "application/force-download";
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
  $mimetype = 'application/x-download';
}

...
// database interactions
...

$downloads_path = 'pages/' . $row['DownloadsPath'];
if (!file_exists($downloads_path . $filename)) {
  die('404 File Not Found');
}
$fileLength = (string) filesize($downloads_path . $filename);
ob_end_clean();

// send file to browser
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: ' . $mimetype);
header('Content-Length: ' . $fileLength);
header("Content-Transfer-Encoding: binary");
readfile($downloads_path . $filename);
At present I've successfully tested it with the following OS/browser combinations:
- Windows XP SP2 / IE7
- Windows XP SP2 / IE6
- Windows XP SP2 / FF 2.0.0.4

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Cheers Clive. I'll implement it over the weekend and add a few more combinations tot he browser matrix :)

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Jeff, let me know how you go.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
It's looking good!

Only needs some function wrappers, header-location (if file is 404), etc. Then you can include it in several pages and simply call the doDownload($filename);

Olav Alexander Mjelde
Admin & Webmaster
 
OK, well I made some very simple changes to the code - left all the headers being delivered as per the code last posted here.

Using MacOSX 10.4, I was able to successfully download and then launch the .exe (by then transferring them to Windows XP):

- Safari 2+, 3+
- Firefox 1+, 2+
- Opera 6.03
- Opera 8+, 9+
- Mozilla 1.7+
- Netscape 7.1
- Camino 1+
- Internet Explorer 5.2.3 (final release for MacOSX)

Note: A "Bad Request" error is presented in 7.54u1 (the last build of Opera 7). I commented out the extra header change that happens on "Opera 7" in the user agent, but got the same thing.

I think you can safely say that the code posted runs in all current web browsers on MacOSX [smile]

I got interested in the same kind of support for Windows XP (Professional) SP 2 and wanted to extend Clive's tests further. I have confirmed the code works in the following:

- Safari 3
- Opera 7+, 8+, 9+
- Netscape 7.1
- IE 5.01

I couldn't get IE 5.5 to launch properly - this is most likely a problem with my local installation.

Note: Opera 7+ appears to be fine in Windows.

Thanks for sharing the code!

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
...and thanks for testing it!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top