PHP implementation of object download instance code

Source: Internet
Author: User
Tags ranges rar readfile

To download a simple object, you only need to use the HTML connection tag <a> and specify the URL value of the href attribute as the downloaded object. The code is as follows:

The code is as follows: Copy code

<A href = "http://www.111cn.net/download/book.rar"> download file </a>

If you download the current code file, you can only process mimetricfiles that cannot be identified by some viewers. For example, when you download the book.rar file, the browser does not open it directly. Instead, a download prompt box is displayed, prompting you to download or open the file. If you download a webpage file with the suffix ".html", an image file, or a PHP script file, the file content is directly output to the browser without prompting you to download the file.
To improve file security, if you do not want to provide the File link in the <a> label, you must send necessary header information to the browser to notify the browser that the file to be downloaded will be processed. PHP uses the header () function to send the header information of a webpage to the browser. This function receives a string of header information as a parameter. The header information to be sent for file download consists of the following three parts, which are completed by calling the three header () function. Taking the downloaded image test.gif as an example, the code for sending the header information is as follows:

The code is as follows: Copy code

Header ('content-Type: imge/GIF'); // sends the header information of the specified file MIME Type
Header ('content-Disposition: attachment; filename={test.gif "'); // sends the header information of the description file, attachment and file name
Header ('content-Length: 3390 & prime;); // sends information about the specified file size, in bytes

If you use header(registry.gif to send these three headers to the browser, the image test.gif will not be displayed directly in the browser, and the browser will form a download form for the file. In the header () function, "Content-Type" specifies the MIME Type of the file, and "Content_Disposition" is used for the description of the file. The value "attachment; filename=”test.gif" indicates that this is an attachment, the downloaded file name is specified, and "Content_Length" indicates the size of the downloaded file.
After setting the header information, you need to output the file content to the browser for download. You can use the file system function in PHP to read the file content and directly output it to the browser. The most convenient is to use the readfile () function to read the file content and output it directly. The code for downloading the file test.gif is as follows:

 

The code is as follows: Copy code
<? Php
$ Filename = "test.gif ";
Header ('content-Type: image/gif '); // specifies the Type of the downloaded file
Header ('content-Disposition: attachment; filename = "'. $ filename.'" '); // specifies the description of the downloaded object
Header ('content-Length: '. filesize ($ filename); // specify the size of the downloaded file
 
// Read the file content and output it directly for download
Readfile ($ filename );
?>


If you encounter a Chinese name, you will not be able to download it normally. For a Chinese name download file, I found another file download instance code.

The code is as follows: Copy code

<? 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 );
?>

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.