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.