Php file download code example,

Source: Internet
Author: User

Php file download code example,

Php File Download Code

To download php files, we need to use the header function to send relevant information to the client browser, and then use the filesize function to read the file size and perform the download operation.
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.

File downloads can only process mimetricfiles that cannot be identified by some viewers. For example, when the book.rar file is published, the browser does not open it directly. Instead, a download prompt box is displayed, prompting users to "Download", "open", and other processing methods. 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. 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
Header ('content-Disposition: attachment; filename={test.gif "'); // sends the header information of the description file, attachment and file name
Header ('content-Length: 3390 '); // 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. Download the test.gif file:
<? 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 );


Articles you may be interested in:
  • PHP File Download instance code
  • Php user-defined function code for forced File Download
  • PHP File Download (header function usage)
  • Php implementation file download instance code
  • When downloading the php header function file, you are prompted to save it.
  • Php supports resumable download (with source code)
  • Php large file download code (supports resumable upload)
  • PHP File Download Code (compatible with multiple browsers and supports Chinese file names)
  • PHP file download function (supports multiple formats)
  • PHP File Download class (supports multiple file types)
  • Php header function implements the instance code for File Download

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, determine whether the specified file exists.
If (! File_exists ($ file_path )){
Echo "this file does not exist ";
$ 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 ( script school)
$ 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: 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.