943,584 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1430
  • PHP RSS
Aug 7th, 2009
0

force download and rename files

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009
Aug 7th, 2009
0

Re: force download and rename files

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
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Aug 7th, 2009
0

Re: force download and rename files

You want to use the following in a new file (download.php)
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
TommyBs is offline Offline
61 posts
since Mar 2008
Aug 7th, 2009
0

Re: force download and rename files

Click to Expand / Collapse  Quote originally posted by TommyBs ...
You want to use the following in a new file (download.php)
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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?
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009
Aug 7th, 2009
0

Re: force download and rename files

try adding readfile at the end
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Aug 7th, 2009
0

Re: force download and rename files

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'){
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Aug 8th, 2009
0

Re: force download and rename files

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
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009
Aug 10th, 2009
0

Re: force download and rename files

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
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Upgrading PHP
Next Thread in PHP Forum Timeline: how to enter data in an array from database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC