PHP implementation file download code sharing _php Tips

Source: Internet
Author: User
Tags php script ranges rar readfile

Simple file downloads require only the use of the HTML connection tag <a>, and the URL value of the attribute href is specified as the downloaded file. As shown:

<a href= "Http://www.jb51.net/download/book.rar" > download file </a>

If the above code to implement file downloads, only some of the browser can not be recognized by default MIME type files, such as when accessing the Book.rar file, the browser does not open directly, but pop-up a download prompt box, prompting the user "download" or "open" and other processing methods. However, if you need to download Web page files, picture files, and PHP script files that have a suffix named. html, you can use this connection form 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, do not want to give a link to the file in the <a> tag, you must send the necessary header information to the browser to notify the browser will be the process of downloading files. 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 to be sent by the file download consists of the following three sections, completed by calling the header () function three times. For example, to download a picture test.gif, you need to send a header message that looks like this:

Header (' content-type:imge/gif '); Sends header headers
(' content-disposition:attachment; filename= ' Test.gif ') of the specified file MIME type;//Send header information, attachment and file name
of the description file Header (' content-length:3390′); Send information of the specified file size, unit byte

If the header () function is used to send three of the wardrobe information to the browser, the picture test.gif will not be displayed directly in the browser, and the browser will have the file form a download. In the header () of the function, "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" describes this as an attachment and specifies the file name after the download, and "Content_length" gives the size of the downloaded file.

After setting the header information, you need to output the contents of the file to the browser for downloading. You can use the file system functions 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. The download file Test.gif is as follows:

 <?php
$filename = "Test.gif";
Header (' content-type:image/gif '); Specifies the download file type
header (' Content-disposition:attachment filename= '. $filename. '); Specifies the description header for the download file
(' 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);
? > 

If the above encounter Chinese name will not be able to download the normal, for the Chinese name download file I found a file download instance code

<?php 
Header ("Content-type:text/html;charset=utf-8"); 
$file _name= "Cookie.jpg"; 
$file _name= "Christmas carnival. jpg"; 
To solve problems in Chinese can not be displayed 
$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 determine is whether the given file exists 
if (!file_exists ($file _path)) { 
echo does not have the file file; 
return; 
} 
$FP =fopen ($file _path, "R"); 
$file _size=filesize ($file _path); 
Header Headers 
("Content-type:application/octet-stream") to be used for downloading files; 
Header ("Accept-ranges:bytes"); 
Header ("Accept-length:". $file _size); 
Header ("content-disposition:attachment; Filename= ". $file _name); 
$buffer =1024; 
$file _count=0; 
Returns 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, tells the browser to display the content in encoded format as UTF-8

About the file_exists () function does not support the Chinese path problem: Because the PHP function is relatively early, does not support Chinese, so if the file name is downloaded in Chinese, you need to convert the character encoding, otherwise the file_exists () function is not recognized, you can use Iconv () function for encoding conversion

$file _sub_path () I use an absolute path, which is more efficient than a relative path.

Header ("Content-type:application/octet-stream") function: Through this Code client browser will be able to know the server side returned the file form 
Header ("Accept-ranges:bytes" Function: Tells the client that the file size returned by the browser is the 
header ("Accept-length:". $file _size) that is calculated by byte: tells the browser to return the file size 
header (" Content-disposition:attachment; Filename= ". $file _name) Role: Tell the browser the name of the file returned 

The above four header () is required
Fclose ($FP) can output the last remaining data in the buffer to the disk file and release the file pointer and related buffers

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.