jim.davidson Posted November 26, 2008 Share Posted November 26, 2008 Why does the following code find the file on one server but cant find it on another? Here's the code, I've used it for one site and works fine, but on the other site can't find the file. // Block any attemp 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. <a href="specs_msds.php">Back</a>.'; if (!$getfile) { // go no further if filename not set echo $nogo; } else { // define the path name to the file // $filepath = '/documents/'.$getfile; // $filepath = $getfile; $filepath = $_SERVER['DOCUMENT_ROOT'].'/documents/'.$getfile; // check that it exist 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/octet-stream'); 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 { $nogo .= $filepath; echo $nogo; } } Quote Link to comment https://forums.phpfreaks.com/topic/134367-solved-file-downloads/ Share on other sites More sharing options...
flyhoney Posted November 26, 2008 Share Posted November 26, 2008 Make sure that all paths and file permissions are the same. Quote Link to comment https://forums.phpfreaks.com/topic/134367-solved-file-downloads/#findComment-699554 Share on other sites More sharing options...
JonnoTheDev Posted November 26, 2008 Share Posted November 26, 2008 print out the filepath variable and check to see if it is what you expect $filepath = $_SERVER['DOCUMENT_ROOT'].'/documents/'.$getfile; print $filepath; exit(); It maybe the additional / after $_SERVER['DOCUMENT_ROOT'] as the server variable may already contain the trailing / Quote Link to comment https://forums.phpfreaks.com/topic/134367-solved-file-downloads/#findComment-699556 Share on other sites More sharing options...
flyhoney Posted November 26, 2008 Share Posted November 26, 2008 Generally double path separators are ignored by operating systems: /path/to/file.ext should be the same as /path//to///file.ext Quote Link to comment https://forums.phpfreaks.com/topic/134367-solved-file-downloads/#findComment-699561 Share on other sites More sharing options...
jim.davidson Posted November 26, 2008 Author Share Posted November 26, 2008 Hey guys, thanks alot, your help was appreciated. It was the extra / that was the problem I change the code from this $filepath = $_SERVER['DOCUMENT_ROOT'].'/documents/'.$getfile; to this $filepath = $_SERVER['DOCUMENT_ROOT'].'documents/'.$getfile; and everything worked. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/134367-solved-file-downloads/#findComment-699708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.