468,733 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

PHP unable to unlink after readfile()

jlm699
314 100+
On my site I have a file that I generate in /var/tmp so that the user can download it. The script is as follows:

Expand|Select|Wrap|Line Numbers
  1. $file_path = '/var/tmp/up2606.csv';
  2.  
  3. header('Cache-Control: public, must-revalidate');
  4. header('Pragma: hack');
  5. header('Content-Type: application/vnd.ms-excel');
  6. header('Content-Length: '.(string)(filesize($file_path)));
  7. header('Content-Transfer-Encoding: binary\n');
  8. header('Content-Disposition: attachment; filename="recv_data.csv"');
  9. readfile($file_path);
  10. unlink($file_path);
  11. ?>
  12.  
So basically what I want to do is delete the file as soon as the user downloads it. (Earlier in the script the file is generated specifically for the user's session).
The script completely ignores the unlink though, as my /var/tmp directory still has all the files that are generated... the funny thing is I'm not getting any errors, so I'm not quite sure how to go about solving this one?
Jan 10 '08 #1
3 4202
jlm699
314 100+
The problem has to be the fact that the user is dl'ing the file. The script executes and probly gives a hidden permission denied error somewhere. I can delete other files using this script, just not the one that's being downloaded...

The script went like this:
Expand|Select|Wrap|Line Numbers
  1. //same download process as before...
  2. $fh = fopen('/var/tmp/foo.bar', 'a');
  3. fputs($fh, 'test');
  4. fclose($fh);
  5.  
  6. unlink('/var/tmp/foo.bar')
  7. unlink($file_path)
  8.  
foo.bar disappeared but the other file did not.
Jan 10 '08 #2
Markus
6,050 Expert 4TB
The file which is executing unlink() has to be in the same directory as the file being deleted :)
Jan 10 '08 #3
jlm699
314 100+
The file which is executing unlink() has to be in the same directory as the file being deleted :)
But I can delete other files in that directory.. just not the one that gets sent to the user.

I'm thinking the only way around this will be to update to postgresql 8.2 so that I will not need to generate a temporary file... either that or make a cron job to clear the .csv files from that directory every day.
Jan 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Eric | last post: by
1 post views Thread by Chamomile | last post: by
4 posts views Thread by nitinpatel1117 | last post: by
4 posts views Thread by Eric Renken | last post: by
1 post views Thread by fcaserio | last post: by
15 posts views Thread by Morteneistrom | last post: by
18 posts views Thread by Coffee Pot | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.