User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,014 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,375 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 916 | Replies: 5
Reply
Join Date: Mar 2008
Posts: 7
Reputation: spiderling is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spiderling spiderling is offline Offline
Newbie Poster

force download

  #1  
Apr 25th, 2008
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.

  1. $filename = $_GET['filename'];
  2. $path = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/";
  3. #$path = "lrg/";
  4. $fullpath = $path.$filename;
  5.  
  6. if(ini_get('zlib.output_compression'))
  7. ini_set('zlib.output_compression', 'Off');
  8.  
  9. header("Pragma: public");
  10. header("Expires: 0");
  11. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  12. header("Cache-Control: private",false);
  13. header("Content-Type: image/jpg");
  14. header("Content-Disposition: attachment; filename=\"".$filename."\";" );
  15. header("Content-Transfer-Encoding: binary");
  16. header("Content-Length: ".filesize($fullpath));
  17. readfile($fullpath);
  18. exit;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Cologne, Germany
Posts: 106
Reputation: sDJh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
sDJh sDJh is offline Offline
Junior Poster

Re: force download

  #2  
Apr 25th, 2008
Use
header("Content-Length: ".$filesize($fullpath));
to add the size of your image as well. It then should work.
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: spiderling is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spiderling spiderling is offline Offline
Newbie Poster

Re: force download

  #3  
Apr 25th, 2008
Originally Posted by sDJh View Post
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.
Reply With Quote  
Join Date: Jul 2006
Location: Remunj
Posts: 173
Reputation: pritaeas is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Junior Poster

Re: force download

  #4  
Apr 28th, 2008
Did you try removing the content-length line ? see php.net/readfile, first comment.
Reply With Quote  
Join Date: Jan 2008
Posts: 71
Reputation: amigura is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
amigura's Avatar
amigura amigura is offline Offline
Junior Poster in Training

Re: force download

  #5  
Apr 28th, 2008
  1. <?
  2. $filename = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/".$_GET['filename'];
  3.  
  4. //$filename="picture0.jpg";
  5.  
  6. if(ini_get('zlib.output_compression'))
  7. ini_set('zlib.output_compression', 'Off');
  8.  
  9. header("Pragma: public");
  10. header("Expires: 0");
  11. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  12. header("Cache-Control: private",false);
  13. header("Content-Type: image/jpeg");
  14. header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
  15. header("Content-Transfer-Encoding: binary");
  16. header("Content-Length: ".filesize($filename));
  17. readfile("$filename");
  18. exit();
  19. ?>
Last edited by peter_budo : Apr 29th, 2008 at 8:42 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Mar 2008
Posts: 7
Reputation: spiderling is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spiderling spiderling is offline Offline
Newbie Poster

Re: force download

  #6  
Apr 28th, 2008
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.

  1. $filename = $_GET['filename'];
  2. $path = $_SERVER['DOCUMENT_ROOT']."/photos/lrg/";
  3. $fullpath = $path.$filename;
  4.  
  5.  
  6. // required for IE, otherwise Content-disposition is ignored
  7. if(ini_get('zlib.output_compression'))
  8. ini_set('zlib.output_compression', 'Off');
  9.  
  10. header("Pragma: public"); // required
  11. header("Expires: 0");
  12. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  13. header("Cache-Control: private",false); // required for certain browsers
  14. header("Content-Transfer-Encoding: binary");
  15. header("Content-Type: image/jpg");
  16. header('Content-Disposition: attachment; filename="'.$filename.'"');
  17. header("Content-Length: ".filesize($fullpath));
  18. readfile($fullpath);
  19. exit;
Last edited by spiderling : Apr 28th, 2008 at 12:42 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:55 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC