Re: Problems downloading a PDF
| From: | Jochem Maas | Date: | Wed, 04 Apr 2007 12:35:00 +0000 |
| Subject: | Re: Problems downloading a PDF | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252030@lists.php.net to get a copy of this message | ||
Richard Davey wrote:
> Mário Gamito wrote:
>
>> To prevent people to insert the full URL to the documents, i moved
>> them outside Apache's DocumentRooot, which is /var/www.
>>
>> My site is in /var/www/telbit and i put the PDFs in /var/www
>>
>> I've coded in order to do the trick, but it's failing.
>> I can't get the name of the file.
>
> You don't need to basename() it, you already know what the filename is,
> because it was requested via $_GET['file'].
I would say almost the opposite:
<?php
if (isset($_SESSION['email'])) {
$error = false;
if (isset($_GET['file'])) {
$file = basename($_GET['file']);
$full = '/var/www/' . $file;
if (!is_readable($full))
$error = "Invalid filename.";)
} else {
$error = "No filename given.";
}
if ($error) {
echo "<a href=\"products-teststudio.php?file=testudio.pdf\"",
" rel=\"external\">Download TESTUDIO flyer</a>";
exit;
}
header('Content-type: application/pdf');
header("Content-Length: " . filesize($full));
header('Content-disposition: attachment; filename="'. $file .'"');
readfile($full);
exit;
} else {
echo "Unauthorized Access!";
}
>
> I would insert a file_exist check before you try and send it. It might
> give you the cause of your problem.
always a good thing
>
> Cheers,
>
> Rich