gbuck Posted July 19, 2009 Share Posted July 19, 2009 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: Sorry, download unavailable. filename is New_Traffic_07_21_2009.pdf filepath is http://localhost/TrafficEntertainment/TE_site/phpsite/batches/New_Traffic_07_21_2009.pdf http://localhost/TrafficEntertainment/TE_site/phpsite/batches/New_Traffic_07_21_2009.pdf is NOT readable http://localhost/TrafficEntertainment/TE_site/phpsite/batches/New_Traffic_07_21_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? Link to comment https://forums.phpfreaks.com/topic/166547-solved-trying-to-force-download-pdf-but-file_exists-returns-false/ Share on other sites More sharing options...
gbuck Posted July 20, 2009 Author Share Posted July 20, 2009 For the record I figured this out after some advice on another forum not to use a URL as the pathname (no http://... ), just the path to the folder on whatever drive it lives on. Link to comment https://forums.phpfreaks.com/topic/166547-solved-trying-to-force-download-pdf-but-file_exists-returns-false/#findComment-878374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.