Jump to content

Recommended Posts

Hi,

 

I thought the following would be an easy task, but it has turned out to be very difficult. I simply want a php script to download an exe file from the server to the client’s browser, which means copying the file to the dowload folder of the client. The exe file is an ordinary wrapper file for installing a program. I have been searching internet for hours and tried dozens of examples of what I thought would accomplish the task, but nothing has worked so far. I have tried

 

readfile($_GET['filename.exe']);

 

fopen("Location: filename.exe", "r");

 

and

 

readfile(’filename.exe’);

The latter just displayed the content of the exe file on the screen, which obviously is not what I want. Though the exe file is in the same directory as the php file on the server, I have also tried to replace the filename with the whole url path. But this didn’t work either. I have also placed all kinds of header functions before the above functions, but nor did this help. Any suggestions for how I could resolve this would be greatly appreciated.
Thanks in advance.
  
Edited by Absolvium

In addition to reading the file contents, you also need to set appropriate headers to tell the browser to save the file. You do this with the header function. There are examples in the manual or all over the internet if you search.

Well, as i wrote, I have spent quite many hours looking for examples on the internet. But if they work, all they do is to download the content of the file to the screen, which is not what I want.

Edited by Absolvium

if you tried code that was had header() statements in it and it didn't download the file, you would need to troubleshoot what is causing the problem. the most likely reasons are -

 

1) outputting something to the browser prior to the header() statements (there would be php detected errors. do you have php's error_reporting/display_errors set to show all php errors?)

 

2) not using the correct headers (you would need to post what you tried for anyone here to be able to help with it.)

Thank you all for your answers. No, I don't know about php's error_reporting/display_errors option. I am not very familiar with php, but am not in need of writing a lot of code either. Just need certain functions to work. I had some output before the header statements, but switched it so that the download came first. Here is one of the options I have tried so far:

 

$file ="testsetup.exe";

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    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));
       
    //readfile($file);
    //fopen($file, "r");
    //readfile($_GET[$file]);

}

 

The readfile and fopen functions downloads the content of the file and thereby crashes the system.

  
 

Edited by Absolvium
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.