This article mainly introduces the PHP safe download file method, involving php file encoding settings, conversion, judgment and download related skills, the need for friends can refer to the next
Specific as follows:
<?phpheader (' content-type:text/html; Charset=utf-8 ');d efine (' Root_path ', DirName (__file__));/** * Download file * @param string $file _path absolute path */function downfile ($ File_path) { //Determine if the file exists $file _path = iconv (' utf-8 ', ' gb2312 ', $file _path);//Transcode If the Chinese name may appear (!file_ Exists ($file _path)) { exit (' file does not exist! '); } $file _name = basename ($file _path); Get the file name $file _size = filesize ($file _path);//Get file size $fp = fopen ($file _path, ' R ');//Open file header in read-only mode (" Content-type:application/octet-stream "); Header ("Accept-ranges:bytes"); Header ("Accept-length: {$file _size}"); Header ("content-disposition:attachment;filename={$file _name}"); $buffer = 1024x768; $file _count = 0; Determines whether the file ends while (!feof ($fp) && ($file _size-$file _count>0)) { $file _data = fread ($fp, $buffer); c17/> $file _count + = $buffer; echo $file _data; } Fclose ($FP); Close File}downfile (Root_path. '/down/sunset.jpg ');? >
Note: The file name can accept the Chinese name. The file format is utf-8.
Summary: The above is the entire content of this article, I hope to be able to help you learn.