PHP implementation of safe download file methods and examples

Source: Internet
Author: User
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.

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.