Usually the file download process is very simple, establish a link to the target file on it. For example, the following link:
xml/html Code
- <a href=http://www.xxx.com/xxx.rar> Click to download the file </a>
however, the situation may be slightly more complex. For example, users need to fill out the full registration information before they can download the file, then the first thought is the use of Redirect. Two ways are described below.
(1) Use the redirect Method. Check that the form is completed and complete, and then refer to the file so that the user can download it. Take a look at the following sample Code:
PHP code
- <?php
- /* file function: Check whether the variable form is complete */
- if ($form) {
- redirect Browser point to
- Header ("location:http://http://www.xxx.com/xxx.rar");
- Exit
- }
- ?>
(2) According to the download file number to find, the form of the link is as Follows:
xml/html Code
- <a href="http://www.xxx.com/download.php?id=123456"> Click to download File </a>
The link above uses the ID to receive the number of the file to be downloaded, and then connects to the actual file link in a redirect Way.
The above two methods, although the implementation of the file download function, but the disadvantage is directly exposed to the path of the file, and there is no anti-theft chain function, so the above way is simple direct but there is a security hidden file download Method. In php, it is common to use the header () function and the fread () function to implement a secure file Download.
For example, to download a file named Xxx.rar file, first create the file is download.php PHP file. From the previous example, it is easy to get the real location of the file to be downloaded from the database by the ID number of the file, which can be redirected directly to the file by the location parameter of the header () function after obtaining the real storage position of the File. however, This is still unsafe, as some download software can still obtain location information for the file through redirection Analysis. So another way to do this is to use Php's file-handling API Functions. It is through the fread () function to output the file directly to the browser prompts the user to download, so that all the processing is done on the server side, so that the user can not get the file specific storage location information, The sample code is as Follows:
PHP code
- <?php
- $file _name = "xxx.rar"; //download file name
- $file _dir = "./up/"; //download File storage directory
- Check if the file exists
- If (! file_exists ( $file _dir. $file _name)) {
- echo "file not found";
- Exit ();
- } Else {
- //open File
- $file = fopen ( $file _dir. $file _name, "r");
- //input File Tags
- Header ( "content-type:application/octet-stream");
- Header ( "accept-ranges:bytes");
- Header ( "accept-length:".) filesize ( $file _dir. $file _name));
- Header ( "content-disposition:attachment; Filename= ". $file _name);
- //output File Contents
- //read file contents and output directly to browser
- echo fread ( $file, filesize ( $file _dir. $file _name));
- Fclose ( $file);
- Exit ();
- }
- ?>
"code interpretation"
In the above code, the program sends the header information to tell Apache and the browser to download the information about the File. The meaning of Content-type means that the file MIME type is a file stream format. If you set the MIME type of the file to Application/octet-stream (such as add Application/octet-stream) in the Apache Configuration. xxx.rar), then the browser (client) will know that this is a file stream format file and prompt the user to Download. Accept-ranges is a response header that allows the server to indicate an acceptance request that will be part of a resource at a given offset and length, which is understood as the unit of measure for the request Scope. Content-length is the length of a byte that specifies the data contained in the request or response, for example, content-length:382. Content-disposition:attachment is used to tell the browser that a file is a value that can be downloaded as an attachment, and the name of the downloaded file is $file_name the Variable.
Run the download.php file, as shown in the Effect. You can see that the file was prompted to download as expected, and click the "save" button to save the file locally.
Original Address: http://www.jizhuomi.com/software/386.html
A detailed explanation of the php file download principle and implementation