Uploading THinkPHP files

Source: Internet
Author: User
The system library file used for downloading THinkPHP1.5 files in THinkPHP is Http. class. php, located in the ThinkPHP \ Lib \ ORG \ Net directory, class name Http, where there is a static method staticfunction THinkPHP file download
The system library file used for downloading THinkPHP1.5 files is Http. class. php, located in the ThinkPHP \ Lib \ ORG \ Net directory, class name Http, where static function download ($ filename, $ showname = ", $ content = ", $ expire = 180); // @ param string $ filename download file name (complete path and File Save name) * @ param string $ showname: Download the name of the displayed file (the name to be displayed or the name to be read from the database in Chinese ); * @ param string $ content the downloaded content (the default value is null. the downloaded file is the original file ). * @ Param integer $ expire: the browser cache time for the downloaded content. the default value is 180 seconds. */Because the PHP file name cannot be saved in Chinese, it is usually saved on the server as an English name or a random name is generated. You can use this method to reply to the original file name during download. Example: the original file name/* is displayed during Download. assume that the file information storage table in the database is file (id, truename, savenane, user, size ). the file exists in the uploads folder under the website project directory. the project name of this website is bm, and its absolute path is H: \ AppServ \ www \ bm \ uploads \ (H: \ AppServ \ www \ is the document root directory. this directory contains a file named 123456789.doc,(savename=, the original file name is "example after reading. Doc", that is, truename, and the size is 2 MB. the program on the server is: class FileAction extends Action {public function download () {$ uploadpath = 'H: \ AppServ \ www \ bm \ uploads \'; // Set the file upload path. the absolute path on the server $ id =$ _ GET ['id']; // The Parameter id uploaded to this method in GET mode, that is, the file in The storage id in the database. find the file information based on it. If ($ id = ") // if the id is empty and an error occurs, the program jumps to the Index/index page of the project. Or do other processing. {$ This-> redirect ('index', 'index', ", APP_NAME,", 1) ;}$ file = D ('file '); // use the data model class FileModel corresponding to the table file to create a data object. $ Result = $ file-> find ($ id); // query the file information by id if ($ result = false) // if the file information cannot be queried and an error occurs, the program jumps to the Index/index page of the project. Or you can do other processing {$ this-> redirect ('index', 'index', ", APP_NAME,", 1) ;}$ savename = $ file-> savename; // file Save name $ showname = $ file-> truename; // file original name $ filename = $ uploadpath. $ savename; // Complete file name (path plus name) import ('org. net. http '); Http: download ($ filename, $ showname) ;}} then add a download link to the location where the file is to be downloaded in the HTML Template downloaded from the file, call the download method of the File module. Remember to upload the Parameter id. In this example:
 
 
Post-reading .doc Sunmoon Download
Note: only gb2312 can be used to display the downloaded file name encoding in IE. Otherwise, the file name is garbled, or the file cannot be downloaded because the file cannot be found. In this case, I made some adjustments to the original download () method. after testing, I found that IE, Aoyou, and firefox can be downloaded normally. /** + ---------------------- * Download file * can specify the file name displayed for download and automatically send the corresponding Header information * if the content parameter is specified, then download the content of this parameter + -------------------- * @ static * @ access public + -------------------- * @ param string $ filename download file name * @ param string $ showname download file name * @ param string $ content download content * @ param integer $ expire download content browser cache time + ---------------------- * @ return void + ---------------------- * @ throws ThinkExecption + ------- --------------- */Static public function download ($ filename, $ showname = '', $ content ='', $ expire = 180) {if (file_exists ($ filename )) {$ length = filesize ($ filename);} elseif (is_file (UPLOAD_PATH. $ filename) {$ filename = UPLOAD_PATH. $ filename; $ length = filesize ($ filename);} elseif ($ content! = '') {$ Length = strlen ($ content);} else {throw_exception ($ filename. L ('The downloaded file does not exist! ');} If (empty ($ showname) {$ showname = $ filename;} $ showname = basename ($ showname); if (empty ($ filename )) {$ type = mime_content_type ($ filename);} else {$ type = "application/octet-stream" ;}// start downloading the Header ("content-type: text/html; charset = utf-8 "); header (" Pragma: public "); header (" Cache-control: max-age = ". $ expire); // header ('cache-Control: no-store, no-Cache, must-revalidate'); head Er ("Expires :". gmdate ("D, d m y h: I: s", time () + $ expire ). "GMT"); header ("Last-Modified :". gmdate ("D, d m y h: I: s", time ()). "GMT"); // the following line is the change, that is, using iconv ("UTF-8", "GB2312 // transcoder", $ showname) the system function is encoded as gb2312 header ("Content-Disposition: attachment; filename = ". iconv ("UTF-8", "GB2312", $ showname); header ("Content-Length :". $ length); header ("Content-type :". $ type); header ('content-Encoding: none '); Header ("Content-Transfer-Encoding: binary"); if ($ content = '') {readfile ($ filename);} else {echo ($ content );} exit () ;}note: iconv is the php system function library, but must be installed. If this module is not installed on the server, download iconv. dll and copy it to windows/system32/. at the same time, copy it to the ext folder in the php installation folder. In the php. ini file, remove ";" before "extension = php_iconv.dll". if not, add "extension = php_iconv.dll. Restart the server.

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.