Created
September 20, 2016 15:24
-
-
Save phpdave/6d32ff605a06994f885605e315eaa0c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$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