Avatar of keith1001
keith1001
Flag for United States of America asked on

PHP - Force file to download - not working

I'm trying to force the download of a file instead of displaying it in a browser and the below code allows me to download the file, but seems not to get the full file.  I can not display any of the jpgs that I download.  Any ideas on what I have wrong?  Keep in mind images may be just one type of a download, as there could be .txt or .pdf etc...

<?php
$file="c:\upload_test\\robot-100_1.jpg"; //file location 
header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);

?>

Open in new window

PHP

Avatar of undefined
Last Comment
Dave Baldwin

8/22/2022 - Mon
Mukesh Yadav

Remove line 7.

File is not downloading because you are saying browser that connection is closed. After receiving this header browser closes connection.
keith1001

ASKER
Even if I remove line 7, same issue.

Also changed line 4 to :  image/jpg

It always appears the file is just a little smaller than what is suppose to be.
Mukesh Yadav

Means you are getting file smaller than it is actually?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mukesh Yadav

Then confirm that you file exists or not?
Julian Hansen

Are you sure there is nothing wrong with your source image?

The code you works perfectly when I tested it on my own image.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

Ray, your script works fine to download image files too in the version I posted above.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
keith1001

ASKER
My script downloads but the image will not display.

I run Ray's script above and it works.
Ray Paseur

Dave, thanks.  I updated the older version recently and added better comments for my teaching library.  In most cases, either version would work correctly.
keith1001

ASKER
Sorry for the delay, and thanks again for your support and working code/answer.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Dave Baldwin

You're welcome.