Skip to content

Instantly share code, notes, and snippets.

@phpdave
Created September 20, 2016 15:24
Show Gist options
  • Save phpdave/6d32ff605a06994f885605e315eaa0c1 to your computer and use it in GitHub Desktop.
Save phpdave/6d32ff605a06994f885605e315eaa0c1 to your computer and use it in GitHub Desktop.
#Rewrite request to www.example.com/my.pdf to www.example.com/OutputPDFinPHP.php?PDFFile=my.pdf t
RewriteEngine on
RewriteRule ^(.*)\.pdf$ OutputPDFinPHP.php?PDFFile=$1 [NC]
<?
$pdfFileNameWithoutExtension = preg_replace("/[^a-zA-Z0-9 ]/", "", $_REQUEST['PDFFile']);
$filename = $pdfFileNameWithoutExtension . '.pdf';
file_exists($filename) ? outputPDF($filename) : echo "File does not exist" ;
function outputPDF($filename)
{
$handle = fopen($filename, "rb");
$filedata = fread($handle, filesize($filename));
fclose($handle);
if (ob_get_length ())
echo 'Data has already been outputed';
if (php_sapi_name() != 'cli')
{
header('Content-Type: application/pdf');
if (headers_sent ())
echo 'Data has already been outputed';
header('Content-Length: ' . strlen($filedata));
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
}
echo $filedata;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment