文件下载与PHP到网页

I develop an webpage and I would like to create an download section. I have an php code to download files, but I have tried it for many times, but it doesn't work. I see continuesly the error message, but the picture.jpg file is in the same directory than the download.php and the index.php. I don't know what the problem is.

The content of download.php:

<?php


if (isset($_GET[‘file’]) && basename($_GET[‘file’]) == $_GET[‘file’]) {

$filename = $_GET[‘file’];

} else {

$filename = NULL;

} 


$err = ‘<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>’; 

if (!$filename) {


echo $err;

} else {


$path = ‘downloads/’.$filename;


if (file_exists($path) && is_readable($path)) {


$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');


$file = @ fopen($path, ‘rb’);

if ($file) {


fpassthru($file);
exit;

} else {

echo $err;

}

} else {

echo $err;

}

}

?>

content of the index.php:

<a href="download.php?file=picture.jpg">Download file</a>

file_exists() and is_readable() do not like relative paths. Use an absolute path instead.

You can probably use $path = $_SERVER['DOCUMENT_ROOT'] . "/$filename";

I think the characters and are screwing it up. Try replacing them with normal single quotes ('), and be more careful when copying code off someone's blog. ;)

I use the following code for my application, which works for me.

if(file_exists($path . $file)){
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"" . $file . "\"");
  header("Content-Length: " . filesize($path . $file)); 
  readfile(base64_decode($path . $file);
}else{
  // No file, do something