Here is a code which retrieves a file (say .doc) from a server, stores it in a temporary folder and then uses an api (say google viewer) to display it and then deletes it.
<?php
$body = "....."; //data from imap server
$name = "abc.doc";
$file = fopen("temp/" . $name,'w');
fwrite($file,$body);
fclose($file);
$url = rawurlencode("http://www.xxx.com/temp/".$name);
// I do not have a direct url to the file on the imap server, thus have to store it in a temporary folder
echo "<iframe src=\"http://docs.google.com/viewer?url={$url}&embedded=true\" width=\"100%\" height=\"100%\" style=\"border: none;\"></iframe>";
unlink("temp/".$name);
?>
Now the issue is that since the php script executes itself first and then echo’s the buffer, the google viewer cannot find the file since its already deleted. Using flush() does not help either.
One work around is to remove the “unlink” command and create a cron-job to delete all files in the temp folder (say after every 2min). Is there a better way to do it?
I’d make the link to
then make viewthis.php stream the file… then as soon as it’s served, you can delete it.