This article mainly introduces PHP secure file download methods, involving PHP file encoding settings, conversion, judgment and download related skills, for more information about how to securely download PHP files, see the following example. We will share this with you for your reference. The details are as follows:
<? Phpheader ('content-Type: text/html; Charset = utf-8 '); define ('root _ path', dirname (_ FILE __)); /*** download file * @ param string $ file_path absolute path */function downFile ($ file_path) {// Determine whether the file exists $ file_path = iconv ('utf-8 ', 'gb2312', $ file_path); // transcode the possible Chinese names if (! File_exists ($ file_path) {exit ('The file does not exist! ') ;}$ File_name = basename ($ file_path); // get the file name $ file_size = filesize ($ file_path); // get the file size $ fp = fopen ($ file_path, 'R'); // open the file header ("Content-type: application/octet-stream") in read-only mode; header ("Accept-Ranges: bytes "); header ("Accept-Length: {$ file_size}"); header ("Content-Disposition: attachment; filename = {$ file_name}"); $ buffer = 1024; $ file_count = 0; // checks whether the object ends. while (! Feof ($ fp) & ($ file_size-$ file_count> 0) {$ file_data = fread ($ fp, $ buffer); $ file_count + = $ buffer; echo $ file_data;} fclose ($ fp); // close the file} downFile (ROOT_PATH. '/down/Sunset.jpg');?>
Note: the file name can be a Chinese name. The file format is UTF-8.