0

I have a php page which downloads zip files. Normally after the download, it automatically revert back the user to a previous page(myfiles.php) using header('location:myfiles.php');.

When I execute the page, it bring me to myfiles.php but the download pop up won't show up, thus preventing me to download my zip file. When I remove the line header('location:myfiles.php');, I am able to download my zip file as expected.

Below is an extract of my code.

    //Some codes
    if(file_exists($zip_name)){

       // push to download the zip
       header('Content-type: application/zip');
       header('Content-Disposition: attachment; filename="'.$zip_name.'"');
       readfile($zip_name);

       // remove zip file is exists in temp path
       unlink($zip_name);
    }        
    session_start();
    $_SESSION['correct']="Files downloaded sucessfully";
    header('location:myfiles.php');

Can you please help me finding a way to fix it? Thank you.

4
  • 2
    readfile outputs data, you can't use header after content has already been sent. The same goes for session_start it should be called before any output. Commented Mar 20, 2019 at 17:22
  • 1
    Upon browser request, you can send a page back or ask it to load a different location; not both. Commented Mar 20, 2019 at 17:25
  • 1
    I think you can not redirect with header after download. Download process is a specific process and it must be a certain page. Also all response ( with headers ) came in one time in your project. So you cant do that with readfile. "May be" if you use send binarys and use ob_flush() function "may" it can be possible. Commented Mar 20, 2019 at 17:31
  • Thank you for the accurate response, i will remove the session part from my code. Commented Mar 20, 2019 at 17:41

1 Answer 1

1

Think about what those headers are doing

These 2 and the readfile

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);

are sending a file to the current page that is on the browser.

This one, which will run as well as the code above

header('location:myfiles.php');

attempts to tell the browser to go to another page. If it did do that then the file you sent to the browser would just disappear

So basically done together as one flow, they do not make sense!

Also you cannot send a header('location:...) after any actual data has been sent to the browser, which of course you did when you ran the readfile($zip_name);

Sign up to request clarification or add additional context in comments.

3 Comments

I seems a bit clearer now that you are explaining this. So basically it won't be possible to automatically be redirected to another page after my download is finished? Or is there another solution for this?
Well remember the user has to be able to react to the UPLOAD and get offered the Save/Open With dialog etc.
I've often seen it done with iframes. As of today, I have no idea whether it clashes with browser security settings (though it probably doesn't).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.