Redirect to a pdf file and then delete on the server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lordrain11
Forum Newbie
Posts: 8
Joined: Wed Nov 19, 2008 7:33 pm

Redirect to a pdf file and then delete on the server

Post by lordrain11 »

Hello, I have a php script that builds a .fdf file and stores it in a folder. No trouble there. Then I was using a header/location redirect to open that file (the .fdf file is linked to a pdf so it opens the pdf filled out). No trouble here either. The issues is there is sensative information in the fdf file so as soon as the user opens it I want it deleted off the server. Basically the user will open this file and print it but in the background I want it deleted from the server. I tried to unlink the file after my redirect but it always unlinks first so then my redirect points to something that doesn't exist. I tried putting a sleep function in but it just delays the unlink and then redirects. I need the redirect to happen first but continue to run the script on the page so it unlinks without the user having to do anything. Here is a snippit:

header ("Location: https://www.mysite.com/formdata/$fdf_file");

unlink("formdata/$fdf_file");

Please help. And if there is a different way I am all ears. Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Redirect to a pdf file and then delete on the server

Post by John Cartwright »

Instead of redirecting, take a look at readfile()
lordrain11
Forum Newbie
Posts: 8
Joined: Wed Nov 19, 2008 7:33 pm

Re: Redirect to a pdf file and then delete on the server

Post by lordrain11 »

The problem I have with readfile is that it just prints out the fdf file on the screen. The fdf needs to be merged with the pdf somewhoe. When you link to a fdf file it automatically brings in the appropriate pdf. For some reason readfile just writes text in the browser. Thoughts?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Redirect to a pdf file and then delete on the server

Post by John Cartwright »

I'll let it slide you did not read the entire documentation page.

Code: Select all

   
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
Post Reply