How to download files securely in PHP _php tips

Source: Internet
Author: User

The example in this article describes the way PHP downloads files securely. Share to everyone for your reference, specific as follows:

 <?php header (' content-type:text/html;
Charset=utf-8 ');
Define (' Root_path ', DirName (__file__)); /** * Download File * @param string $file _path absolute path/function Downfile ($file _path) {//To determine whether a file exists $file _path = iconv (' Utf-8 ' , ' gb2312 ', $file _path); transcoding if (!file_exists ($file _path) for a possible Chinese name) {exit (' file does not exist!
  '); } $file _name = basename ($file _path); Gets the file name $file _size = filesize ($file _path); Gets 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;
    To determine whether the file is closed while (!feof ($fp) && ($file _size-$file _count>0)) {$file _data = fread ($fp, $buffer);
    $file _count + + $buffer;
  echo $file _data; } fclose ($FP); Closes the file} downfile (Root_path. '/down/sunset.jpg ');?> 

Description: The name of the file can be accepted in Chinese. The file format is utf-8.

More interested in PHP related content readers can view the site topics: "Php Curl Usage Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.