PHP implementation File Download code example, _php tutorial

Source: Internet
Author: User
Tags ranges readfile

PHP implementation of a file download code example,


PHP implementation file download code

PHP implementation file Download We need to use the header function to send the relevant information to the client browser, and then combine the FileSize function to read the file size and download operations.
Simple file download only needs to use the HTML connection tag, and specify the URL value of the attribute href as the downloaded file.

File download, only can be processed by some browsers can not be recognized by default MIME type file, for example, when accessing the Book.rar file, the browser does not open directly, but instead pops up a download prompt box, prompting the user to "download" or "open" processing methods. However, if you need to download a Web page file with a suffix. html, a picture file, and a php program script file, you can use this form of connection to output the contents of the file directly to the browser without prompting the user to download it.
In order to improve the security of the file, you do not want to give the link to the file in the tag, you must send the necessary header information to the browser to inform the browser of the processing of the downloaded file. PHP uses the header () function to send the header information of a Web page to the browser, which receives a string of header information as a parameter. The header information that the file download needs to send consists of the following three parts, which are completed by calling the three header () function. To download the image test.gif as an example, you need to send the header information as shown:
Header (' content-type:imge/gif '); Sends header information for the specified file MIME type
Header (' content-disposition:attachment; filename= ' test.gif '); Send header information, attachments, and file names for a profile
Header (' content-length:3390′ '); Send information about the size of the specified file, in units of bytes

If you use the header () function to send this three of costume information to the browser, the image test.gif will not be displayed directly in the browser, and the browser will have the file form the download form. In the function header (), "Content-type" specifies the MIME type of the file, "Content_disposition" is used for the description of the file, the value "attachment; Filename= "Test.gif" "indicates that this is an attachment and specifies the file name after the download," Content_length "gives the size of the downloaded file.
After the header information is set, the contents of the file need to be exported to the browser for download. You can use the file system function in PHP to read the contents of the file and output it directly to the browser. The most convenient is to use the ReadFile () function to read the contents of the file directly output. Download the file Test.gif as shown in:
$filename = "Test.gif";
Header (' content-type:image/gif '); Specify the download file type
Header (' content-disposition:attachment; Filename= '. $filename. '); Specify a description of the download file
Header (' Content-length: '. FileSize ($filename)); Specify the size of the download file

Read the contents of the file and output it directly for download
ReadFile ($filename);
?>

Articles you may be interested in:

    • PHP File Download Instance code
    • PHP Mandatory File Download Custom function code
    • PHP File Download (header function usage)
    • PHP implementation file Download instance code
    • PHP header function File download direct prompt to save
    • PHP support for the continuation of the file download type (source)
    • PHP large File download code (support breakpoint continuation)
    • PHP File download code (multi-browser compatible, support Chinese file name)
    • PHP File download functions (multiple formats supported)
    • PHP File Download class (multiple file types supported)
    • PHP header function Implementation of file download instance code

Above if encountered Chinese name will not be normal download, for Chinese name download file I found a file download instance code

Header ("Content-type:text/html;charset=utf-8");
$file _name= "Cookie.jpg";
$file _name= "Christmas carnival. jpg";
To solve problems that cannot be displayed in Chinese
$file _name=iconv ("Utf-8", "gb2312", $file _name);
$file _sub_path=$_server[' Document_root ']. " Marcofly/phpstudy/down/down/";
$file _path= $file _sub_path. $file _name;
The first thing to tell is whether a given file exists or not
if (!file_exists ($file _path)) {
echo "does not have this file";
return;
}
$FP =fopen ($file _path, "R");
$file _size=filesize ($file _path);
The header to use to download the file
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;
Returning data to the browser
while (!feof ($fp) && $file _count< $file _size) {
$file _con=fread ($fp, $buffer);
$file _count+= $buffer;
echo $file _con;
}
Fclose ($FP);
?>
The function of the header ("Content-type:text/html;charset=utf-8"): When the server responds to the browser's request, tells the browser to display the content in encoded format as UTF-8 encoding
About the file_exists () function does not support the Chinese path problem: Because the PHP function is earlier, does not support Chinese, so if the file name is downloaded in Chinese, it needs to be converted to character encoding, otherwise the file_exists () function is not recognized, you can use the Iconv () function for encoding conversion (www.jbxue.com Script Academy)
$file _sub_path () I'm using an absolute path, and execution is more efficient than a relative path.
The function of the Header ("Content-type:application/octet-stream"): Through this code the client browser can know the file form returned by the server
The role of the Header ("Accept-ranges:bytes"): tells the client that the file size returned by the browser is calculated in bytes
Header ("Accept-length:". $file _size): tells the browser the size of the file returned
Header ("content-disposition:attachment; Filename= ". $file _name): Tell the browser the name of the file returned

The above four headers () are required
Fclose ($FP) can output the last remaining data in the buffer to a disk file, releasing the file pointer and the associated buffer




http://www.bkjia.com/PHPjc/866465.html www.bkjia.com true http://www.bkjia.com/PHPjc/866465.html techarticle PHP Implementation File download code example, PHP implementation file download code PHP implementation file Download We need to use the header function to send relevant information to the client browser, and then combine file ...

  • Related Article

    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.