| | |
force download and rename files
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 82
Reputation:
Solved Threads: 5
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
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
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
desktop_write
You want to use the following in a new file (download.php)
change the filename parameter to what they called the file in the db and use the correct extension. Also change the content type header
PHP Syntax (Toggle Plain Text)
// We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf 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
•
•
Join Date: Apr 2009
Posts: 82
Reputation:
Solved Threads: 5
•
•
•
•
You want to use the following in a new file (download.php)
PHP Syntax (Toggle Plain Text)
// We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf 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
PHP Syntax (Toggle Plain Text)
$ext = substr(strrchr($file, "."), 1); if ($ext == pdf){ $ctype="application/pdf"; } if ($ext == doc){ $ctype="application/msword"; } if ($ext == xls){ $ctype="application/vnd.ms-excel"; } if ($ext == ppt){ $ctype="application/vnd.ms-powerpoint"; } // We'll be outputting a PDF header('Content-type: '.$ctype.''); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="'.$name.'.'.$ext.'"'); } ?>
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?
try adding readfile at the end
PHP Syntax (Toggle Plain Text)
..... header('Content-Disposition: attachment; filename="'.$name.'.'.$ext.'"'); readfile($file); ?>
Last edited by paradox814; Aug 7th, 2009 at 11:01 pm. Reason: added code tags
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'){
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'){
•
•
Join Date: Apr 2009
Posts: 82
Reputation:
Solved Threads: 5
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
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
![]() |
Similar Threads
- FTP - Download multiple files using ruby (Ruby)
- Get a Mac v1.0 Virus (Viruses, Spyware and other Nasties)
- bulk rename files (IT Professionals' Lounge)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- force download (PHP)
- need help deserately! virus won't go away! (Viruses, Spyware and other Nasties)
- ie.content can't delete files (Web Browsers)
- Rename a Series of Files (Windows tips 'n' tweaks)
Other Threads in the PHP Forum
- Previous Thread: Upgrading PHP
- Next Thread: how to enter data in an array from database
| Thread Tools | Search this Thread |
advanced apache api array beginner binary broken cakephp check checkbox class cms code cookies cron curl database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google head href htaccess html if...loop image include includingmysecondfileinthechain insert ip javascript job joomla jquery key library limit link login mail menu mlm multiple mysql oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search server sessions smarty sms soap sorting source space sql startup stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube zend





