tags:

views:

414

answers:

6

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?

Thanks for any suggestions.

A: 

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:) )

pbrodka
+1  A: 

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();

Kyle Cronin
+1  A: 

http://us2.php.net/ignore_user_abort

Be very careful using this, you can pretty quickly kill a server by abusing it.

David
+4  A: 

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.

Darryl Braaten
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.
TravisO
+1  A: 

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.

David
A: 

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.

lo_fye