Php file download function for detailed process analysis
Source: Internet
Author: User
The browser sends a request to access a webpage (such as down. php) on the server. the code for this webpage is as follows:
The browser sends a request to access a webpage (such as down. php) on the server. the code for this webpage is as follows.
The server immediately runs the down. php file after receiving the request.
When running the file, you must read the file to be downloaded into the memory (here is the picture of Christmas Carnival .jpg). here, you can use the fopen () function to complete this action.
Note: Any file downloading from the server must be read into the memory on the server first.
Now the file is in the memory. this is to read the file from the memory and use the fread () function to complete this action.
It should be noted that if the file is large, the file should be divided into multiple sections and returned to the client. it is not returned to the client at one time after all the files are read on the server, this will increase the load on the server.
Therefore, we need to set the number of bytes read at a time in the php code. for example, in the following code, we use $ buffer = 1024 to set the number of bytes read at a time, output data (that is, returned to the browser)
Flowchart:
The code is as follows:
Header ("Content-type: text/html; charset = utf-8 ");
// $ File_name = "cookie.jpg ";
$ File_name = "Christmas Carnival .jpg ";
// Solve the problem that Chinese characters cannot be displayed
$ File_name = iconv ("UTF-8", "gb2312", $ file_name );
$ File_sub_path = $ _ SERVER ['document _ root']. "marcofly/phpstudy/down /";
$ File_path = $ file_sub_path. $ file_name;
// First, determine whether the specified file exists.
If (! File_exists ($ file_path )){
Echo "this file does not exist ";
Return;
}
$ Fp = fopen ($ file_path, "r ");
$ File_size = filesize ($ file_path );
// The header used to download the object
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". $ file_size );
Header ("Content-Disposition: attachment; filename =". $ file_name );
$ Buffer = 1024;
$ File_count = 0;
// Return data to the browser
While (! Feof ($ fp) & $ file_count <$ file_size ){
$ File_con = fread ($ fp, $ buffer );
$ File_count + = $ buffer;
Echo $ file_con;
}
Fclose ($ fp );
?>
Notes:
Header ("Content-type: text/html; charset = utf-8"): When the server responds to a browser request, it tells the browser to display the Content in the encoding format of the UTF-8.
The file_exists () function does not support Chinese paths. because php functions are relatively early and do not support Chinese characters, if the downloaded file name is Chinese, character encoding and conversion are required, otherwise, the file_exists () function cannot be recognized. you can use the iconv () function for encoding conversion.
$ File_sub_path () I use absolute paths, which are more efficient than relative paths.
Header ("Content-type: application/octet-stream"): the client browser can use this code to know the file format returned by the server.
Header ("Accept-Ranges: bytes"): indicates that the file size returned by the client browser is calculated in bytes.
Header ("Accept-Length:". $ file_size): indicates the size of the file returned by the browser.
Header ("Content-Disposition: attachment; filename =". $ file_name): indicates the name of the file returned by the browser.
The preceding four headers () are required.
Fclose ($ fp) can output the last remaining data in the buffer to the disk file, and release the file pointer and the related buffer.
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.