Php implements file download code sharing

Source: Internet
Author: User
To download php files, we need to use the header function to send the relevant information to the client browser, and then use the filesize function to read the file size and perform the download operation. Let's take a look at the relevant examples.

To download php files, we need to use the header function to send the relevant information to the client browser, and then use the filesize function to read the file size and perform the download operation. Let's take a look at the relevant examples.

To download a simple object, you only need to use the HTML connection tag and specify the url value of the href attribute as the downloaded object. As shown in:

Download files

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 a file link in the tag, you must send the 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. Take the downloaded image test.gif as an example. The header information to be sent is shown in:

Header ('content-Type: imge/gif'); // sends the header information of the specified file MIME Type ('content-Disposition: attachment; filename=”test.gif "'); // sends the header information of the description file, the attachment and the file name header ('content-Length: 3390 '); // sends the information of 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. Download the test.gif file:

<? Php $ filename = "test.gif"; header ('content-Type: image/gif'); // specify the download file Type header ('content-Disposition: attachment; filename = "'. $ filename. '"'); // specifies the description header ('content-Length :'. filesize ($ filename); // specify the size of the downloaded file // read the file content and output it directly to 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.

<? 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, you must determine whether the specified file exists. if (! File_exists ($ file_path) {echo "this file is not available"; return ;}$ fp = fopen ($ file_path, "r"); $ file_size = filesize ($ file_path ); // Header ("Content-type: application/octet-stream") required to download the file; 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 Header returned by the server ("Accept-Ranges: bytes ") purpose: Tell the client browser that the returned file size is the Header ("Accept-Length :". $ file_size): Tell the browser to return the file size Header ("Content-Disposition: attachment; filename = ". $ file_name): 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.