PHP download file contains Chinese name

Source: Internet
Author: User
Tags php download
This article mainly introduces the PHP download file contains the Chinese name, has a certain reference value, now share to everyone, the need for friends can refer to


Controller code:

Public Function SaveFile () {//receive the file name that needs to be downloaded/*if (!isset ($_get[' file ')) exit (' Filename is empty '); if (Empty ($_get[' file ')) exit (' Filename not valid '); */$file = I (' get.file ', ' ', ' trim ');//$file = '/publi        C/upload/files/20180426/abc.zip ';//$file = '/public/upload/files/20180426/123.zip ';        $new _name = explode ('/', $file);        $new _name = $new _name[count ($new _name)-1];        $file _name = $new _name;        Ob_clean ();//clear buffer//Get file name $filename = basename (UrlDecode ($file)); The full path of the file (where the real file is stored in the temp directory)//$filePath = __dir__. "        /temp/". $filename;        $root = DirName (dirname (DirName (dirname (__file__))); $filePath = $root.        $file;        Convert the UTF8 encoding to GBK encoding, otherwise the file with the Chinese name cannot be opened $filePath = Iconv (' UTF-8 ', ' GBK ', $filePath);        Checks if the file is readable if (!is_file ($filePath) | |!is_readable ($FILEPATH)) exit (' Can not Access file '. $filename);  /** * This should include code such as security verification, such as: detecting the source of the request, verifying the UA identity, etc.       *//Open the file as read-only and force binary mode $fileHandle = fopen ($filePath, "RB");        if ($fileHandle = = = False) {exit ("Can not open File: $filename"); }//The file type is a binary stream.        Set to UTF8 encoding (supports Chinese file name) header (' Content-type:application/octet-stream; Charset=utf-8 ');        Header ("Content-transfer-encoding:binary");        Header ("Accept-ranges:bytes"); File size Header ("Content-length:".)        FileSize ($filePath));        Trigger browser file Download function//header (' Content-disposition:attachment;filename= '. UrlEncode ($filename). '); Header (' content-disposition:attachment;filename= '. $file _name.        '"'); Iterates through the contents of the file and outputs the while (!feof ($fileHandle)) {//reads up to length bytes (10k per output) from the file pointer handle echo Frea        D ($fileHandle, 10240);    }//Close File stream fclose ($fileHandle); } page and Js:<p style= "padding:5px 10px 0px 5px" ><span style= "float:left;" >{$vo. Name} </span><a class= "Down" style= "Float:right" data-url= "__root__/{$vo. Pic_url} "download=" {$vo. Pic_url} "> Download </a></p><script>mui (' body '). On (' Tap ', '. '), function () {var url = $ (this). attr ("Data-url"); window.location.href = ' __controller__/savefile?file= ' + URL;}); </script>

Here JS is the MUI box, H5 can be changed to the Click event

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.