Absolvium Posted March 8, 2015 Share Posted March 8, 2015 (edited) 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 March 8, 2015 by Absolvium Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/ Share on other sites More sharing options...
Ch0cu3r Posted March 8, 2015 Share Posted March 8, 2015 If you want the server to force a file download then you need to send the appropriate headers using header. Google "PHP force download" for examples, there are many guides out there. Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/#findComment-1507892 Share on other sites More sharing options...
kicken Posted March 8, 2015 Share Posted March 8, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/#findComment-1507893 Share on other sites More sharing options...
Absolvium Posted March 9, 2015 Author Share Posted March 9, 2015 (edited) 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 March 9, 2015 by Absolvium Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/#findComment-1507928 Share on other sites More sharing options...
mac_gyver Posted March 9, 2015 Share Posted March 9, 2015 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.) Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/#findComment-1507937 Share on other sites More sharing options...
Absolvium Posted March 9, 2015 Author Share Posted March 9, 2015 (edited) 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 March 9, 2015 by Absolvium Quote Link to comment https://forums.phpfreaks.com/topic/295173-cant-download-exe-file-from-server-to-clents-browser/#findComment-1507965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.