PHP Safe Download File method, _php tutorial

Source: Internet
Author: User
Tags getting started with php php download

PHP Safe Download File method,


This article explains how to safely download files in PHP. Share to everyone for your reference, 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.

More readers interested in PHP related content can view the topic: "PHP Curl Usage Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document tips summary (including word, excel,access,ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP implementation of the SAE on the use of storage upload and download files method
    • PHP enables remote download of files to local
    • PHP implementation of image files and download file anti-theft chain method
    • PHP download file code sharing compatible with IE6 browser
    • PHP download File source code (force arbitrary file format download)
    • Example of a method that automatically adds a BOM header when PHP downloads a file
    • Resolve the problem of how to solve garbled characters in PHP download file name
    • PHP Download file-based explanation
    • Code examples for php download files
    • Code to force the download of files in PHP (solve the problem of Chinese file name garbled under IE)
    • PHP to do the download file implementation code and file name garbled solution method

http://www.bkjia.com/PHPjc/1119992.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119992.html techarticle php Safe Download File method, this article describes the PHP safe download file method. Share to everyone for your reference, specifically as follows: Phpheader (' content-type:text/html; Charset=u ...

  • 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.