Jump to content

Recommended Posts

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;

  }
}

Link to comment
https://forums.phpfreaks.com/topic/134367-solved-file-downloads/
Share on other sites

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 /

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.