I am trying to make PDF files available to my users but not others. The suggestion seems to be to put these above site root but below web root. The first step is to test that the file exists and the result is always FALSE. Question: Why does file_exists always return false? Things tried 1. created directory called "a" (to avoid misspelling) in web root. file_exists("/a/") is false file_exists("/a/info.pdf") is false although the file exists 2. clearstatcache(); before file_exists 3. added allow_url_fopen = on to php.ini in root
enter code here
clearstatcache();
$full_path = '/a/info.pdf'; // absolute physical path to file below web root.
if ( file_exists($full_path) )
{
$mimetype = 'application/pdf';
header('Cache-Control: no-cache');
header('Cache-Control: no-store');
header('Pragma: no-cache');
header('Content-Type: ' . $mimetype);
header('Content-Length: ' . filesize($full_path));
$fh = fopen($full_path,"rb");
while (!feof($fh)) { print(fread($fh, filesize($full_path))); }
fclose($fh);
}
else die("File does not exist on the server - .");
The else is always followed. What else can I try?
First you must read document of file_exists()
If you didn't find your mistake, then check folder files with scandir()
For example:
print_r(scandir('/a/'));
Then you can see your file isn't there.
Your problem maybe one of this:
when you use full path like you did /a/file.pdf, actually you trying to get file from the root folder of entire server, and not the DOCUMENT_ROOT of you account in the server.
for example:
you have account and the public folder is:
/home/{ACCOUNT_NAME}/public_html/index.php
so when you trying "/a/file.pdf" you don't get :
/home/{ACCOUNT_NAME}/a/file.pdf
you need to define and check by absolute path in you account only,
for example as i wrote, use full path with you account:
/home/{ACCOUNT_NAME}/a/file.pdf
or even use relative path if you trying from index.php file:
../a/file.pdf
if you don't know the full path to your account,
see what you have in : $_SERVER['DOCUMENT_ROOT'] variable