Php file download function for detailed Process Analysis

Source: Internet
Author: User

Analysis on the process of downloading files from the client server:

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:

Copy codeThe Code is as follows:
<? Php
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.

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.