Jump to content
Jimity

File download appearing as data within page - not downloading

Recommended Posts

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);

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 :(

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 by Jimity

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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.