I want to download a file from a folder above web root. Let's say I want to get this file:
/var/pdfs/test.pdf
My code is:
$name = "test.pdf";
$url = "/var/pdfs/" . $name;
if (file_exists($url)) {
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename='" . basename($url) . "'");
readfile($url);
} else {
echo "Not found: $url";
}
The problem is that I always get the Not found: $url
echo.
My file has read permissions:
thecrafter@vps136166:/var/pdfs$ ls -la
total 6484
drwxr--r-- 2 thecrafter root 4096 Jul 7 00:19 .
drwxr-xr-x 13 root root 4096 Jul 7 00:16 ..
-rw-r--r-- 1 thecrafter thecrafter 6629018 Jul 7 00:18 test.pdf
and directory too:
thecrafter@vps136166:/var$ ls -la | grep pdfs
drwxr--r-- 2 thecrafter root 4096 Jul 7 00:19 pdfs
Do I miss something? Where is the problem?
EDIT 1
Krzysztof Duszczyk noted that the problem is solved if I give execute rights to pdfs
directory. Indeed I gave it 755
and it's working. Any ideas why that happens?