PHP secure file download method ,. PHP secure file download method. This article describes the PHP secure file download method. For your reference, see phpheader (Content-Type: texthtml; Charsetu PHP secure file download method,
This document describes how to securely download PHP files. 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.