Jump to content

[SOLVED] file downloads


jim.davidson

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

 

 

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.