Jimity Posted August 14, 2015 Share Posted August 14, 2015 Hi All, I'm hoping someone can help me out on this one. I cant seem to figure out why its happening. I have a HTML/PHP form that takes in a series of user input and populates them into a series of placeholders within some source files. Those source files, edited with the placeholders, are then to be saved as new files and archived into a zip file which is sent to the user as a download. Everything up to this point, the saving of the new source files and creation of the zip file, works fine. Next, I want to display a new page with a success message to the end user, which in turn starts the download of the zip file. In order to carry the name of the file between pages I'm using $_SESSION. Rather than downloading the file though, the binary content of that zip file displays within this page along with the success message. The file doesn't download. Below is the coding (I've done shortened variations of it with the same result). If I run this coding, or shortened versions of it, within the original HTML/PHP form page (without a success message page) it works fine. Can anyone help me out with where I'm going wrong ? $myfile = $_SESSION['zipfile']; header('Content-Description: File Transfer'); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename='.basename($myfile)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($myfile)); readfile($myfile); Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/ Share on other sites More sharing options...
scootstah Posted August 15, 2015 Share Posted August 15, 2015 You're probably sending output before the headers causing them to not send. And you probably don't have error reporting turned on or you would see that. Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1518864 Share on other sites More sharing options...
Jimity Posted August 15, 2015 Author Share Posted August 15, 2015 Hi Scootstah, I've moved this PHP coding to the very top of my page and turned on Error Reporting. Unfortunately no errors come up and I get the same result Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1518870 Share on other sites More sharing options...
scootstah Posted August 15, 2015 Share Posted August 15, 2015 So if you put this at the very top of your code, you don't get errors? ini_set('display_errors', 'On'); error_reporting(-1);Are you using output buffering? Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1518872 Share on other sites More sharing options...
Jimity Posted August 15, 2015 Author Share Posted August 15, 2015 (edited) Hi Scootstah, The good news is its fixed. Well . . . sort of I ran the code in my script as you recommended. It found problems with session_start. Even though it was at the top of my code, there were spaces there, so I removed all spaces and placed it at the very top of my code. This led me to a new error though.: Cannot modify header information - headers already sent. After doing a lot of reading up on this one, and not believing it could be true, I found the php file was being encoded with Byte-Order-Mark, which was causing this error. After downloading Notepad++ and re-saving the file, I'm now happy to say it works. The file comes in as a download rather than its binary contents displayed within the browser. Now I have a new problem though. . . The HTML that makes up the success message within this file for the end user is not displaying. Instead, the original form stays on screen, with the file download taking place. As I dont have any header coding for downloading the file in the original (starting) form page, I know the browser is going to my success message page. I just cant get the text of that page to show. Should I make a new post for this problem ? Edited August 15, 2015 by Jimity Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1518977 Share on other sites More sharing options...
CroNiX Posted August 17, 2015 Share Posted August 17, 2015 That damn BOM Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1519123 Share on other sites More sharing options...
Jimity Posted September 11, 2015 Author Share Posted September 11, 2015 Apologies for the delay in replying. I fixed it with a bit of ugly coding Basically I've shown the HTML file contents, and from that I've run a meta tag calling (a second later) the PHP coding (in a seperate file) that starts the download. Its ugly IMHO, but it works I now have another issue, but I'll start a new post for that Quote Link to comment https://forums.phpfreaks.com/topic/297794-file-download-appearing-as-data-within-page-not-downloading/#findComment-1520694 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.