note 79419 deleted from function.header by danbrown
| From: | danbrown@php.net | Date: | Sun, 07 Dec 2008 18:08:55 +0000 |
| Subject: | note 79419 deleted from function.header by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147803@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at askapache dot com
----
What if you want to serve all .pdf files on your site without changing all the links to point to a
php script? The solution is to use Apache .htaccess and mod_rewrite with the php script.
[ .htaccess ]
+======================================+
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+)\.pdf$ /cgi-bin/pdf.php?file=$1 [L,NC,QSA]
+======================================+
[ PHP Script pdf.php ]
+======================================+
if(!isset($_GET['file']))die('LOGGED! no file specified');
$file_path=$_SERVER['DOCUMENT_ROOT'].
'/'.strip_tags(htmlentities($_GET['file'])).'.pdf';
if ($fp = fopen ($file_path, "r")) {
$file_info = pathinfo($file_path);
$file_name = $file_info["basename"];
$file_size = filesize($file_path);
$file_extension = strtolower($file_info["extension"]);
if($file_extension!='pdf') {
die('LOGGED! bad extension');
}
ob_start();
header('Content-type: application/pdf');
header('Content-Disposition: attachment;
filename="'.$file_name.'"');
header("Content-length: $file_size");
ob_end_flush();
while(!feof($fp)) {
$file_buffer = fread($fp, 2048);
echo $file_buffer;
}
fclose($fp);
exit;
exit();
} else {
die('LOGGED! bad file '.$file_path);
}
+======================================+
original article:
http://www.askapache.com/htaccess/pdf-plugin-adobe.html