want to force-download PDF, but "file_exists" returns false
Posted: Sun Jul 19, 2009 4:45 pm
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:
the echo results:
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 file surely exists to populate the drop-down box in the first place, and $filepath returns the correct filepath. Any ideas?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