Home > Backend Development > PHP Tutorial > An example of PHP file download code, _PHP tutorial

An example of PHP file download code, _PHP tutorial

WBOY
Release: 2016-07-13 10:20:41
Original
754 people have browsed it

An example of php file download code,

php file download code

To implement file downloading in PHP, we need to use the header function to send relevant information to the client browser, and at the same time combine it with the filesize function to read the file size and perform the download operation.
To download a simple file, you only need to use the HTML connection tag and specify the URL value of the href attribute as the downloaded file.

File download can only process some MIME type files that the browser cannot recognize by default. For example, when accessing the book.rar file, the browser does not open it directly, but pops up a download prompt box, prompting the user to "Download" or "Open" and other processing methods. However, if you need to download web page files, image files, PHP program script files, etc. with the suffix .html, using this connection form, the file content will be output directly to the browser and the user will not be prompted to download.
In order to improve the security of the file, if you do not want to give a link to the file in the
tag, you must send the necessary header information to the browser to notify the browser that the file is about to be downloaded. PHP uses the header() function to send the header information of the web page to the browser. This function receives a string of header information as a parameter. The header information that needs to be sent for file download includes the following three parts, which is completed by calling the header() function three times. Take downloading the picture test.gif as an example. The header information that needs to be sent is as follows:
header('Content-Type:imge/gif'); //Send the header information of the specified file MIME type
header(' Content-Disposition:attachment; filename="test.gif"'); //Send header information describing the file, attachment and file name
header('Content-Length:3390′); //Send the specified file size Information, unit byte

If you use the header() function to send these three lines of header information to the browser, the image test.gif will not be displayed directly in the browser, but the browser will format the file as a download. In the function header(), "Content-Type" specifies the MIME type of the file, "Content_Disposition" is used to describe the file, and the value "attachment; filename="test.gif"" indicates that this is an attachment and specifies the download After the file name, "Content_Length" gives the size of the downloaded file.
After setting the header information, the content of the file needs to be output to the browser for downloading. You can use the file system function in PHP to read the file content and output it directly to the browser. The most convenient way is to use the readfile() function to read the file content and output it directly. Download file test.gif as shown:
$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 download file

//Read the file content and output it directly for downloading
readfile($filename);
?>

Articles you may be interested in:

  • php file download example code
  • php custom function code to force file download
  • php file download (header function usage)
  • php file download example code
  • When downloading the php header function file, you are prompted to save it directly
  • php file download class that supports breakpoint resumption (source code attached)
  • php large file download code (supports breakpoint resume download)
  • php file download code (compatible with multiple browsers, supports Chinese file names)
  • PHP file download function (supports multiple formats)
  • php file download class (supports multiple file types)
  • Example code for file download using php header function

If you encounter a Chinese name above, it will not download normally. For downloading files with Chinese names, I found another file download example code

header("Content-type:text /html;charset=utf-8");
// $file_name="cookie.jpg";
$file_name="Christmas Carnival.jpg";
//Used to solve the problem that Chinese cannot be displayed The problem
$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 such 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;
//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 the browser's request, it tells the browser to encode The content is displayed in UTF-8 format
About the problem that the file_exists() function does not support Chinese paths: Because the php function is relatively early and does not support Chinese, so if the downloaded file name is in Chinese, it needs to be modified Character encoding conversion, otherwise the file_exists() function cannot recognize it, you can use the iconv() function for encoding conversion (www.jbxue.com Script Academy)
$file_sub_path() I use an absolute path, which is more efficient than a relative path The role of
Header("Content-type: application/octet-stream"): Through this code, the client browser can know the file format returned by the server
Header("Accept-Ranges: bytes" ): Tell the client that the file size returned by the browser is calculated in bytes
Header("Accept-Length:".$file_size): Tell the browser the file size returned
Header( The role of "Content-Disposition: attachment; filename=".$file_name): tells the browser the name of the file returned

The above four Header() are necessary
fclose($fp) can output the last remaining data in the buffer to the disk file and release the file pointer and related buffer

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/866465.htmlTechArticleAn example of php file download code, php file download code php file download we need to use the header function to send Relevant information is given to the client browser, and combined with the file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template