I have a PDF that needs to be accessible for multiple unrelated areas across a website. I don’t want to reveal anything about where the file is downloaded from and I want to only upload the file once.
Is it possible to serve a file stored in the private space, as opposed to storing it in a public directory. If so, can someone point me in the correct direction or give an example?
1
PHP provides the int fpassthru(resource $handle) function. You open a file, and then have fpassthru send the file outby simply copying it to the output without storing it, allowing potentially very large files to be sent.
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
If you can, it is better to avoid using PHP to do anything other than setting up the initial download, and then handing off the actual download to the webserver to control (and so release a PHP process). If you control the server, you can install/enable mod_xsendfile (for Apache) or the Nginx equivalent, X-accel.
Yes. Keywords are: file streaming with php.
It is something like this:
echo file_get_contents('secretfolder/the_file.pdf');
Do a search or have a look at:
https://stackoverflow.com/questions/6914912/streaming-a-large-file-using-php