Jump to content

[SOLVED] Trying to force-download PDF, but file_exists returns false


gbuck

Recommended Posts

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:

<?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:

 

The file surely exists to populate the drop-down box in the first place, and $filepath returns the correct filepath.  Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.