Antonio1471 Posted February 1, 2023 Share Posted February 1, 2023 function Download_Delete_File($filename,$destPos) { ignore_user_abort (true); set_time_limit ( 0 ); $file_path = './'.$filename; $file_type = 'text/csv'; $file_name = $filename; //header ( 'Cache-Control: max-age=31536000' ); //header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); header ( 'Content-Length: ' . filesize ( $file_path ) ); //header ( 'Content-Disposition: filename="' . $file_name . '"' ); header ( 'Content-Type: ' . $file_type . '; name="' . $file_name . '"' ); header('Content-disposition: attachment;filename="' . $file_name . '"'); ob_clean(); flush(); readfile ( $file_name ); unlink ( $destPos ); unlink ( $file_name ); exit (); } Hi all, I have used the code above to do a download of a file generated by PHP code on the client and it works. Now I have the problem of making the browser write more echoes to indicate that the file is in the download folder and some other information. I have tried removing the exit command to make the PHP code continue but it does not work. I also tried ob_start, $continue=ob_get_clean, but I can't see the rest of the code on the browser. It just stops at the start page without continuing. Do you have a suggestion to continue the php script so that it shows me the rest of the print functionthat is called after the download? I also tried calling the print function first, but to no avail. Do you have any ideas that can help me? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/ Share on other sites More sharing options...
kicken Posted February 1, 2023 Share Posted February 1, 2023 You cannot send any more information other than the downloaded file's content. Any other information you try and send will just be part of the download. If you want a thank you message or similar to appear you either need to show it before you start the download, or after a delay using javascript. Before is easier, which is why you'll often see places take to you a page that read like: Quote Thank you for downloading XYZ, your download should begin shortly. They show that page, then issue a redirect to the download URL so the browser will start the download. Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/#findComment-1605260 Share on other sites More sharing options...
Antonio1471 Posted February 1, 2023 Author Share Posted February 1, 2023 22 minutes ago, kicken said: You cannot send any more information other than the downloaded file's content. Any other information you try and send will just be part of the download. If you want a thank you message or similar to appear you either need to show it before you start the download, or after a delay using javascript. Before is easier, which is why you'll often see places take to you a page that read like: They show that page, then issue a redirect to the download URL so the browser will start the download. Thank you for your reply. I tried printing before calling the download function but it doesn't work. You recommend that I put the function in a new php page, thank the user, and then a redirect to the new php page that effectuates the download and deletion of the file, correct? But how can I pass 2 variables to the download page? I need to indicate the name of the file to download and delete. Now I call the function like this : Download_Delete_File($filename,$destPos); Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/#findComment-1605261 Share on other sites More sharing options...
Antonio1471 Posted February 1, 2023 Author Share Posted February 1, 2023 I tried putting the function in a new php page and calling it with : header("Location: ./anotherFile.php"); and first print THANK YOU. It still won't print it. If I flush it prints it, but it doesn't call up the new downoload php page. Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/#findComment-1605263 Share on other sites More sharing options...
Solution kicken Posted February 1, 2023 Solution Share Posted February 1, 2023 If you use a header redirect then the browser will not render the output of that request and instead just fetch the next page. So you need to delay the redirect by returning a normal response that the browser will render and have that page then perform the redirect. That delay can be accomplished by using either JavaScript or a meta refresh to issue the redirect after the page has loaded. Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/#findComment-1605264 Share on other sites More sharing options...
Antonio1471 Posted February 2, 2023 Author Share Posted February 2, 2023 Hi kicken, I followed your advice and inserted a http-equiv meta on the download_and_delete.php page with 1 second delay By doing this I have the file data and a thank you printed. Thank you very much for your advice. Bye Antonio Quote Link to comment https://forums.phpfreaks.com/topic/315866-output-on-browser-after-ob_clean-command-and-flush/#findComment-1605275 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.