I have to open and then unlink a pdf document.

$url = "../tempdocs/".$tmpdocname;
header("Location: ".$url."");

here i can open this document but as I add unlink($url); the browser shows " File is not found".

so unlink runs before the completion of file open...

if anybody knows a solution please reply

Recommended Answers

All 3 Replies

wouldnt you need to open the pdf in read only in your script ?

Member Avatar for diafol

Does anything run after the header() to redirect? Is the file being deleted or have you got a problem with your location?
Do you redirect successfully if you comment out the unlink statement?

The problem is (if I am reading this correctly) that if you intend to redirect the user to the file, you can't delete it first.

The redirect happens after the PHP code completes, so any call to unlink($url); anywhere in your code would delete the file... and then your redirect would send him to where it used to be.

A solution: you could turn the response into a PDF file (in simple terms) print the file contents, and then throw it away.
Like:

<?php
$pdfFile = '/path/to/file.pdf';

// Turn this response into a PDF file
header('content-type: application/pdf');
header('content-disposition: attachment; filename="'. basename($pdfFile) . '"');
header('content-length: '. filesize($pdfFile));

// Print the contents of the file
readfile($pdfFile);

// Delete the file.
unlink($pdfFile);
?>

You might want to add some error checking to that before using it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.