PHP secure file download method _ PHP

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

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.