A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearContinue running a php script after page is returned - page 1
User InfoPosts
Continue running a php script after page is returned#1
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);

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?

posted date: 2008-12-10 13:15:00

Re: Continue running a php script after page is returned#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-10 13:15:01

Re: Continue running a php script after page is returned#3
You can try doing smth like that:<iframe src="<?=$url?>"></iframe>....<?sleep(10);unlink($url);?>Other option is to use curl - then you load file in request and display to the user. Question - do you want to delete the file that user cannot have it - I'm afraid it's impossible, when user loads file it is temporaly in his browser - so he can save it.Next option - if you know type of this file, you can generate content/type header so user will download the file. And then you delete it.It's just simple ideas, don't know which will work for you( if any:) )

posted date: 2008-12-10 13:28:00

Re: Continue running a php script after page is returned#4
The only way I've discovered to do this so far is to provide the content length in the header. Try adding this:header("Content-Length: 0");before your flush();

posted date: 2008-12-10 13:31:00

Re: Continue running a php script after page is returned#5
http://us2.php.net/ignore_user_abortBe very careful using this, you can pretty quickly kill a server by abusing it.

posted date: 2008-12-10 14:02:00

Re: Continue running a php script after page is returned#6
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.

posted date: 2008-12-10 14:04:00

Re: Continue running a php script after page is returned#7
Alternatively.... instead of messing with dynamically generating files on the fly... why not make a handler like so:tempFile.php?key={md5 hash}tempFile.php then either queries a DB, memcache ( with additional prepended key ), or apc for the content.

posted date: 2008-12-10 14:05:00

Re: Continue running a php script after page is returned#8
Yes, creating a page and then destroying it is a waste of resources, just print this file to the user, kills two birds with 1 stone. If you are trying to do something like output an CSV, printing it to the browser is still the best way.

posted date: 2008-12-10 14:57:00

Re: Continue running a php script after page is returned#9
You're going about this the wrong way. You can create the file and serve it to them, and delete it in one step.<?php$file_contents = 'these are the contents of your file';$random_filename = md5(time()+rand(0,10000)).'.txt';$public_directory = '/www';$the_file = $public_directory.'/'.$random_filename;file_put_contents($the_file, $file_contents);echo file_get_contents($the_file);unlink($the_file);?>If you do it that way, the files get deleted immediately after the user sees them. Of course, this means that the file need not exist in the first place. So you could shorten the code to this:<?php$file_contents = 'these are the contents of your file';echo $file_contents;?>It all depends on where you're getting the content you want to show them. If it's from a file, try:<?php$file_contents = file_get_contents($filename_or_url);echo $file_contents;?>As for deleting files automatically, just setup a cron job that runs every 10 seconds, and deletes all the files in your temp folder that where filemtime($filename) is greater than 5 minutes' worth of seconds.

posted date: 2008-12-10 18:41:00

Re: Continue running a php script after page is returned#10
If you want to implement your original design, read this question about running a command in PHP that is "fire and forget" Asynchronous shell exec in PHP

posted date: 2011-02-23 18:53:00

Re: Continue running a php script after page is returned#11
As seen at \Symfony\Component\HttpFoundation\Response::send/** * Sends HTTP headers and content. * * @return Response * * @api */public function send(){ $this->sendHeaders(); $this->sendContent(); if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } elseif ('cli' !== PHP_SAPI) { // ob_get_level() never returns 0 on some Windows configurations, so if // the level is the same two times in a row, the loop should be stopped. $previous = null; $obStatus = ob_get_status(1); while (($level = ob_get_level()) > 0 && $level !== $previous) { $previous = $level; if ($obStatus[$level - 1]) { if (version_compare(PHP_VERSION, '5.4', '>=')) { if (isset($obStatus[$level - 1]['flags']) && ($obStatus[$level - 1]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)) { ob_end_flush(); } } else { if (isset($obStatus[$level - 1]['del']) && $obStatus[$level - 1]['del']) { ob_end_flush(); } } } } flush(); } return $this;}

posted date: 2015-09-11 01:27:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.