I've tried the following force download script and for the life of me I can't get it to download the file. I get the download dialog pop-up, but the file size is only around 500 bytes. I think it is the pathing. I've tried absolute and relative paths for readfile and filesize. The thumbnail page and download.php file are in the photos folder. The file(s) I want to download are images, IMG1.jpg, IMG2.jpg, etc. So, the link users click to download is: download.php?filename=IMG1.jpg.

Your help is much appreciated.

$filename = $_GET['filename'];
$path = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/";
#$path = "lrg/";
$fullpath = $path.$filename;

if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: image/jpg");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($fullpath));
readfile($fullpath);
exit;

Use

header("Content-Length: ".$filesize($fullpath));

to add the size of your image as well. It then should work.

Use

header("Content-Length: ".$filesize($fullpath));

to add the size of your image as well. It then should work.

Thanks, but that didn't work. The filesize in the pop-up dialog is now 139 bytes.

Did you try removing the content-length line ? see php.net/readfile, first comment.

Member Avatar for amigura
<?
$filename = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/".$_GET['filename'];

//$filename="picture0.jpg";

if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Pragma: public");
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false);
header("Content-Type: image/jpeg"); 
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($filename)); 
readfile("$filename"); 
exit();
?>

I decided to browser directly to one of the 'lrg' photos and see if it came up - it didn't. So, I looked in the folder via FTP and they are there. After staring at them for a few minutes I noticed that the JPG extension had capital letters. When I browsed and ran the script I was using lower case letters. The larger photos were upper case because they were the original photos from the camera and the thumbnails are the ones I created.

I did try everyones suggestions before figuring out the little detail of letter cases. Thanks for your help.

The code I am using below works.

$filename = $_GET['filename'];
$path = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/";
$fullpath = $path.$filename;


// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/jpg");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Length: ".filesize($fullpath));
readfile($fullpath);
exit;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.