Small php file download instance

Source: Internet
Author: User
This file download instance is very detailed. it combines the header function and the whilefread function to split the file and read it before sending it to the client. this is a standard file download instance. A small php file download instance *... this file download instance is very detailed. it is combined with the header function and the while fread function to split the file and read it and then send it to the client. it is considered a standard file download instance.

A small php file download instance

/* ===================================================== =====================$ FileName is the file name, $ FilePath is required as the file path. optional. it can be a relative path or an absolute path consisting of only English letters and data, cannot contain Chinese characters. if you have any questions, please contact the blogger and point out ====================================== ======================================= */
 1) {if (substr ($ FilePath, 0, 1) = '/') {// Determine whether the path is an absolute path $ FilePath = $ _ SERVER ['document _ root']. $ FilePath;} if (substr ($ FilePath,-1 )! = "/") {// Check whether the end is/$ FilePath = $ FilePath. '/';} if (is_numeric (strpos ($ FilePath, ":") {// check whether it is an absolute path $ FilePath = str_replace ("/","", $ FilePath) ;}} elseif (strlen ($ FilePath) ==1 & $ FilePath! = "/") {$ FilePath = $ FilePath. "/";} else {$ FilePath = "";} if (! File_exists ($ FilePath. $ FileName) {echo "Download failed: the file to be downloaded is not found"; return ;} /* ===================================================== ============ header information related to sending and downloading ========================== =========================================*/header ("Content-type: application/octet-stream "); header (" Accept-Ranges: bytes "); // return the header (" Accept-Length: $ FileSize ") in bytes "); // return the file size header ("Content-Disposition: attachment; filename = ". $ FileName); // The pop-up dialog box of the client, corresponding File name ============== start Download =======================================*/$ FileSize = filesize ($ FilePath. $ FileName); $ File = fopen ($ FilePath. $ FileName, "r"); // open the File $ FileBuff = 512; while ($ FileSize> = 0) {$ FileSize-= $ FileBuff; echo fread ($ File, $ FileBuff);} fclose ($ File);?>

Summary

This download instance supports Chinese characters and uft8 encoding at the beginning of the file.

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.