php Implementation of File Download Code Sharing

  • 2021-07-10 19:16:13
  • OfStack

Simple file download only needs to use the connection tag of HTML < a > And specify the URL value of the attribute href as the downloaded file. As shown:

< a href= "https://www.ofstack.com/download/book.rar" > Download a file < /a >

If the file download is realized through the above code, only one MIME type file that the browser cannot recognize by default can be processed. For example, when accessing the book. rar file, the browser does not open it directly, but pops up a download prompt box to prompt the user to "download" or "open". However, if you need to download web page files, picture files and PHP program script files with the suffix. html, using this connection form, the file contents will be directly output to the browser, and the user will not be prompted to download.

In order to improve the security of files, you do not want to use the < a > Tag, you must send the necessary header information to the browser to inform the browser that the file will be downloaded. PHP uses the header () function to send the page's header information to the browser, which takes a string of header information as an argument. The header information to be sent for file download includes the following three parts, which are completed by calling header () function three times. Take the download picture test. gif as an example. The header information to be sent is as follows:


header( ' Content-Type:imge/gif'); // Send the specified file MIME Header information of type 
header( ' Content-Disposition:attachment; filename= " test.gif "' ); // Send header information, attachment and file name describing the file 
header( ' Content-Length:3390 ' ); // Sends information for the specified file size, in bytes 

If you use the header () function to send the 3-line information to the browser, the image test. gif will not be displayed directly in the browser, but the browser will download the file. In the function header (), "Content-Type" specifies the file's MIME type, "Content_Disposition" is used to describe the file, and the value "attachment; filename=" test. gif "indicates that this is an attachment and specifies the file name after downloading, 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 function in PHP to read out the contents of the file and output it directly to the browser. The most convenient is to use the readfile () function to read out the contents of the file and output it directly. Download the file test. gif as shown:


 <?php
$filename = "test.gif";
header('Content-Type:image/gif'); // Specify the download file type 
header('Content-Disposition: attachment; filename="'.$filename.'"'); // Specify the description of the downloaded file 
header('Content-Length:'.filesize($filename)); // Specify the size of the downloaded file 
 
// Read the contents of the file and output it directly for download 
readfile($filename);
?> 

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


<?php 
header("Content-type:text/html;charset=utf-8"); 
// $file_name="cookie.jpg"; 
$file_name=" Christmas Carnival .jpg"; 
// It is used to solve the problem that Chinese cannot 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; 
// First, determine whether the given file exists or not  
if(!file_exists($file_path)){ 
echo " There is no file for this file "; 
return ; 
} 
$fp=fopen($file_path,"r"); 
$file_size=filesize($file_path); 
// Headers needed to download files  
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); 
?>

header ("Content-type: text/html; charset=utf-8 "): When the server responds to the browser's request, it tells the browser to display the content in the encoding format of UTF-8

About the problem that file_exists () function does not support Chinese path: Because php function is relatively early and does not support Chinese, if the downloaded file name is Chinese, it is necessary to carry out character encoding conversion, otherwise file_exists () function cannot recognize it, so iconv () function can be used 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") The function of: Through this code, the client browser can know the file form returned by the server  
Header("Accept-Ranges: bytes") Tells the client browser that the file size returned 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) The role of : Tell the browser the name of the file returned  

The above four Header () are required
fclose ($fp) outputs the last remaining data in the buffer to a disk file and frees the file pointer and associated buffer


Related articles: