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.
readfileoutputs data, you can't use header after content has already been sent. The same goes forsession_startit should be called before any output.sessionpart from my code.