Download exe file from server-PHP Scripts

After reading the php.net’s header manul, I wrote the scripts named
download_page.php, it worked. That is, when a visitor clicked the link to the download_page.php, the file-save dialog was shown, and the file “MyProgram.exe”
was downloaded.

Below scripts is named as “download_page.php”

<?
$size=filesize(“MyProgram.exe”);
header(“Content-Type: application/exe”);
header(“Content-Disposition: attachment; filename=MyProgram.exe”);
header(“Content-Length: $size”);
header(“Cache-control: private”);

$filename=“MyProgram.exe”;
$fp =fopen($filename, ‘r’);
fpassthru($fp);

header(“Location: MyProgram.exe”);

?>

I have 2 questions.

  1. the for-download file name shown on the prompted file-save dialog
    is “download_page.php”, not “MyProgram.exe”,
    How can resolve this error?

  2. header(“Location: MyProgram.exe”);
    Is this line really needed in the download_page.php?

Thanks for any help!

  1. When you click “Save” in the dialog does it save the download_page.php file or the MyProgram.exe??

  2. What that line is doing is actually redirecting to the program file itself. I honestly don’t know if it is necessary or not but that is the beauty of PHP, try it without it and see what happens :slight_smile:

Hello, the scripts began to work correctly after I put comment on the three lines in the file.

<?
$size=filesize(“MyProgram.exe”);
header(“Content-Type: application/exe”);
header(“Content-Disposition: attachment; filename=MyProgram.exe”);
header(“Content-Length: $size”);
header(“Cache-control: private”);

//$filename=“MyProgram.exe”;
//$fp =fopen($filename, ‘r’);
//fpassthru($fp);

header(“Location: MyProgram.exe”);

?>

Sure. With your previous solution, the data gets send through the php file to your browser.
The header-redirect just redirects your browser to the correct file, and you also don’t need the header-manipulation (Content-Type, Content-Disposition, Content-Length, Cache-Control) except Location.

Thank you all!