views:

39

answers:

2

Hi!

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>
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";

Scott Saunders
I tried this: $path = $_SERVER{'DOCUMENT_ROOT'} . "/$filename";, but unfortunatelly the error is the same
DaveDetent
+1  A: 

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. ;)

no