Jump to content

Open pdf file with redirect then delete on server immediately


lordrain11

Recommended Posts

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.

I think what you should do is push the file to the browser using the following (http://us2.php.net/readfile):

 

<?php
$file = 'formdata/' . $fdf_file;

if (file_exists($file)) {
    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));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

// unlink file
unlink("formdata/$fdf_file");

?>

 

This should allow you to delete the file afterwards.

Thanks for the quick response but I am still having a few issues.  First, the unlink isn't working.  Its like once the file gets

pushed to the brower it stops reading the script and never executes the unlink.  Any thoughts?

 

Secondly, doing it this way opens up Acrobat and then proceeds to open the actual document up inside my exisitng window.

I wanted it to open in a seperate window so when user was done with file they could just close it and continue on the site.

I even tried opening this page in a new window (target_blank whent the user clicks) but the pdf file always opens in the original

page. 

After a bit more playing around it seems the exit; command is what is causing the unlink to not work.  If I comment it out then

it deletes the file.  However, when I don't have exit; then the pdf won't open I get an I/O error.  Maybe that helps you?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.