Page 1 of 1

want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 4:45 pm
by gbuck
I have a drop-down box in a form populated with filenames from a folder using PHP. I want to force-download with submit button. Seems to work until i run "file_exists" and "is_readable", which i strange because my $getfile and $filepath variables seem to be correct.

my download.php:

Code: Select all

<?php
// block any attempt to explore the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
  $getfile = $_GET['file'];
  }
else {
  $getfile = NULL;
  }
// define error handling
$nogo = 'Sorry, download unavailable.';
 
if (!$getfile) {
  // go no further if filename not set
  echo $nogo;
  }
else {
  // define the pathname to the file
  $filepath = 'http://localhost/TrafficEntertainment/TE_site/phpsite/batches/'.$getfile;
  // check that it exists and is readable
  if (file_exists($filepath) && is_readable($filepath)) {
    // get the file's size and send the appropriate headers
    $size = filesize($filepath);
    header('Content-Type: application/pdf');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment; filename='.$getfile);
    header('Content-Transfer-Encoding: binary');
   // open the file in binary read-only mode
   // suppress error messages if the file can't be opened
    $file = @ fopen($filepath, 'rb');
    if ($file) {
     // stream the file and exit the script when complete
      fpassthru($file);
      exit;
      }
   else {
     echo $nogo;
     }
   }
  else {
    echo $nogo;
   }
  }
 
echo "<br />";
echo "filename is ".$getfile;
echo "<br />";
echo "filepath is ".$filepath;
echo "<br />";
if(is_readable($filepath))
  {
  echo ("$filepath is readable");
  }
else
  {
  echo ("$filepath is NOT readable");
  }
echo "<br />";
if(file_exists($filepath))
  {
  echo ("$filepath exists");
  }
else
  {
  echo ("$filepath does NOT exist");
  }
?>
the echo results:
Sorry, download unavailable.
filename is New_Traffic_07_21_2009.pdf
filepath is http://localhost/TrafficEntertainment/T ... 1_2009.pdf
http://localhost/TrafficEntertainment/T ... 1_2009.pdf is NOT readable
http://localhost/TrafficEntertainment/T ... 1_2009.pdf does NOT exist
The file surely exists to populate the drop-down box in the first place, and $filepath returns the correct filepath. Any ideas?

Re: want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 6:59 pm
by requinix
Don't use a URL as the path to the file. Use whatever it is on the hard drive.

So if localhost is based out of /var/www/htdocs then you should be using /var/www/htdocs/TrafficEntertainment/.../$getfile as the path.

Re: want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 8:54 pm
by gbuck
Amazing! Totally works, thank you so much. So what would be the correct path on the remote server?

Re: want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 9:34 pm
by requinix
The correct path to what?

Re: want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 10:15 pm
by gbuck
Your advice worked perfectly on my local testing server, but I can't get it to work on the live remote server, must be a pathname issue I guess. The fully-qualified URL won't work either. Thanks...

Re: want to force-download PDF, but "file_exists" returns false

Posted: Sun Jul 19, 2009 10:28 pm
by gbuck
Just a recap: it works now after a little trial-and-error for the proper path on the server, but without your advice tasairis it would have taken me forever to figure out. Thank you.

Re: want to force-download PDF, but "file_exists" returns false

Posted: Mon Jul 20, 2009 3:34 am
by jackpf
You can just use $_SERVER['DOCUMENT_ROOT'] to allow it to work on all servers, no matter how the files are arranged.