force download and rename files

Thread Solved

Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training

force download and rename files

 
0
  #1
Aug 7th, 2009
ok what i have users do is upload files to the server, the files gets renamed for conflicting issues, and then they choose a name they want to call the file. After it uploads it sends that info to the database.

now i know that they are going to want to download that file but i don't know how to do a force download. Not only that but i would like to change the file name that they are going to download (not on the server) to there user friendly name.

i have searched the web but i find things that are more complicated than i think i needs to be or i don't understand it.

so i am asking all of you genius's for a way to do this. I think the idea of it would have to be on another file that executes it all
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 213
Reputation: chrishea is an unknown quantity at this point 
Solved Threads: 32
chrishea's Avatar
chrishea chrishea is offline Offline
Posting Whiz in Training

Re: force download and rename files

 
0
  #2
Aug 7th, 2009
You can use my desktop_write module. As you will see, the actual code to do it is pretty simple. This just packages it for you and provides the option of different output file types.

desktop_write
Chris
See my list of PHP development tools at:
InnovationsDesign.net
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: TommyBs is an unknown quantity at this point 
Solved Threads: 11
TommyBs's Avatar
TommyBs TommyBs is offline Offline
Junior Poster in Training

Re: force download and rename files

 
0
  #3
Aug 7th, 2009
You want to use the following in a new file (download.php)
  1. // We'll be outputting a PDF
  2. header('Content-type: application/pdf');
  3. // It will be called downloaded.pdf
  4. header('Content-Disposition: attachment; filename="downloaded.pdf"');

change the filename parameter to what they called the file in the db and use the correct extension. Also change the content type header
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training

Re: force download and rename files

 
0
  #4
Aug 7th, 2009
Originally Posted by TommyBs View Post
You want to use the following in a new file (download.php)
  1. // We'll be outputting a PDF
  2. header('Content-type: application/pdf');
  3. // It will be called downloaded.pdf
  4. header('Content-Disposition: attachment; filename="downloaded.pdf"');

change the filename parameter to what they called the file in the db and use the correct extension. Also change the content type header
thanks that works on what i needed. of course with a little bit more coding to do some things

  1. $ext = substr(strrchr($file, "."), 1);
  2.  
  3. if ($ext == pdf){
  4. $ctype="application/pdf";
  5. }
  6. if ($ext == doc){
  7. $ctype="application/msword";
  8. }
  9. if ($ext == xls){
  10. $ctype="application/vnd.ms-excel";
  11. }
  12. if ($ext == ppt){
  13. $ctype="application/vnd.ms-powerpoint";
  14. }
  15.  
  16. // We'll be outputting a PDF
  17. header('Content-type: '.$ctype.'');
  18. // It will be called downloaded.pdf
  19. header('Content-Disposition: attachment; filename="'.$name.'.'.$ext.'"');
  20.  
  21.  
  22. }
  23. ?>

Now i have the problem that it is not downloading correctly. i transfered the file off my server and they work perfectly. but when i run this script it downloads as a corrupt or empty file

any thoughts?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: force download and rename files

 
0
  #5
Aug 7th, 2009
try adding readfile at the end
  1. .....
  2. header('Content-Disposition: attachment; filename="'.$name.'.'.$ext.'"');
  3. readfile($file);
  4. ?>
Last edited by paradox814; Aug 7th, 2009 at 11:01 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: force download and rename files

 
0
  #6
Aug 7th, 2009
also just to optimize the php code you have, I would switch to a switch statement instead of going through a ton of if statements.

Also, the code you entered didn't parse correctly, did it? I don't see any single quotes around the constant literals
if ($ext == 'pdf'){
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training

Re: force download and rename files

 
0
  #7
Aug 8th, 2009
thanks for the tip.

now i think i know what the problem is. it is not finding the document cause it is in the folder. now i did try to put it in the filename but it just outputs the directory with the name.

i was looking through some of the complex examples that saw but did not see there they are getting the directory.

i am stumped
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training

Re: force download and rename files

 
0
  #8
Aug 10th, 2009
bump...

would anyone know how to specify the directory that the files would be in?

i tried to just put it in the directory but it gave me a server error
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC