Jump to content

Output on Browser after ob_clean command and flush


Go to solution Solved by kicken,

Recommended Posts

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

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.

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

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.

  • Solution

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. 

 

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.