Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue running a php script after page is returned

Tags:

php

I'm trying to write a script that will create a file on the server then use header() to redirect the user to that file. Then, after about 10 seconds I want to delete the file. I've tried this:

header('Location: '.$url);
flush();
sleep(10);
unlink($url);

But the browser just waits for the script to complete then gets redirected, but the file hes been deleted by that time. Is there someway to tell the browser "end of file", then keep computing? Or maybe have PHP start another script, but not wait for that script to finish?

like image 772
user13557 Avatar asked Dec 10 '08 21:12

user13557


1 Answers

You might be better off having the PHP page serve the file. No need to create a temporary file in this case and delete it, just send out the data you intended to write to the temporary file. You will need to set the headers correctly so the browser can identify the type of file you are sending. i.e. Content-Type: text/xml; for xml or image/jpeg for jpg's.

This method also handles slow clients that take longer to download the file.

like image 129
Darryl Braaten Avatar answered Oct 23 '22 03:10

Darryl Braaten