Page 1 of 1

Redirect to a pdf file and then delete on the server

Posted: Tue Dec 16, 2008 5:46 pm
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.

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

Posted: Tue Dec 16, 2008 9:47 pm
by John Cartwright
Instead of redirecting, take a look at readfile()

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

Posted: Tue Dec 16, 2008 9:59 pm
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?

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

Posted: Tue Dec 16, 2008 10:02 pm
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);