A detailed explanation of the php file download principle and implementation

Source: Internet
Author: User
Tags fread ranges vars

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
    1. <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
    1. <?php
    2. /* file function: Check whether the variable form is complete */
    3. if ($form) {
    4. redirect Browser point to
    5. Header ("location:http://http://www.xxx.com/xxx.rar");
    6. Exit
    7. }
    8. ?>

  (2) According to the download file number to find, the form of the link is as Follows:

xml/html Code
    1. <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
  1. <?php
  2. $file _name = "xxx.rar"; //download file name
  3. $file _dir = "./up/"; //download File storage directory
  4. Check if the file exists
  5. If (! file_exists ( $file _dir. $file _name)) {    
  6. echo "file not found";
  7. Exit ();
  8. } Else {
  9. //open File
  10. $file = fopen ( $file _dir.    $file _name, "r");
  11. //input File Tags
  12. Header ( "content-type:application/octet-stream");
  13. Header ( "accept-ranges:bytes");
  14. Header ( "accept-length:".) filesize ( $file _dir.    $file _name));
  15. Header ( "content-disposition:attachment; Filename= ".    $file _name);
  16. //output File Contents
  17. //read file contents and output directly to browser
  18. echo fread ( $file, filesize ( $file _dir.    $file _name));
  19. Fclose ( $file);
  20. Exit ();
  21. }
  22. ?>

  "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

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.