Re: Problems downloading a PDF
| From: | Zoltán Németh | Date: | Wed, 04 Apr 2007 12:08:19 +0000 |
| Subject: | Re: Problems downloading a PDF | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252027@lists.php.net to get a copy of this message | ||
2007. 04. 4, szerda keltezéssel 12.46-kor Mário Gamito ezt írta:
> Hi,
>
> I'm building this site that has several PDFs to download, but only to
> registered users.
>
> 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.
> Notice that the page which has the code is products-teststudio.php itself.
>
> The code follows my signature.
>
> Any help would be appreciated.
>
> Warm Regards
> --
> :wq! Mário Gamito
> --
>
> <?php
> if (isset($_SESSION['email'])) {
> echo"<a href=\"products-teststudio.php?file=testudio.pdf\"
> rel=\"external\">Download TESTUDIO flyer</a>";
you cannot do this. you are sending output here, html output, so the
webserver sends the html headers for it. and later you try to send a pdf
and corresponding headers in the same script. that will never work.
greets
Zoltán Németh
>
> $file = $_GET['file'];
>
> // try to sanitize the filename
> if (preg_match('/[^A-Za-z0-9._]/', $file))
> die("Invalid filename.");
>
> $path = '/var/www/';
> $full = $path . $file;
>
> header('Content-type: application/pdf');
> header("Content-Length: " . filesize($full));
> header('Content-disposition: attachment; filename="'. basename($file)
> .'"');
> readfile($full);
> }
>