2

I am having trouble uploading a file to the user from outside of "public_html" which is the folder that everyone has access to thru http. Ex. www.website.com/ everyhting after the / is public_html. I'm sure you all know what I'm referring to.

So I have this script that should read a file (sample.pdf) from (server perspective) /images/protected/ but PHP won't find my file. Am I doing this wrong?

<?php
$file = '/images/protected/sample.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
} else {
    echo "404 - FILE NOT FOUND!<br />\n";
    echo "File: $file";
}
?>

Every time I execute this script I get the 404 - FILE NOT FOUND!<br />\n instead of a file download.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Axel Latvala
  • 576
  • 3
  • 7
  • 21
  • Are you really sure that that's the correct path? It seems odd that a directory called "images" would be in the root. – JJJ Oct 24 '11 at 20:29
  • Does the httpd process have rights to see or read that file? – Tim Oct 24 '11 at 20:29
  • He is probably listing the absolute web path, not the absolute system path. – Tim Oct 24 '11 at 20:30
  • @Akke can you login to that box and cd to the "images" directory and show us the output of `pwd`? – Tim Oct 24 '11 at 20:31

2 Answers2

4

You have to supply the absolute system path. Currently, you're requesting a file at /images/..., rather than /var/www/hosts/whateverdomain/images/....

$file = $_SERVER['DOCUMENT_ROOT'].'/images/protected/sample.pdf';

If your file is really located at /images/protected/, make sure that you have sufficient permissions to read that file.

Rob W
  • 341,306
  • 83
  • 791
  • 678
0

Turns out I am on shared hosting so the right "root" path for my website was /home/me/

Axel Latvala
  • 576
  • 3
  • 7
  • 21