Home > Article > Backend Development > How to use PHP to implement the download function
The content shared with you in this article is about how to use PHP to implement the download function. The content is of great reference value and I hope it can help friends in need.
PHP
The file is download.php
, and the file for download is 1.jpg
.
<?php $filename="1.jpg"; if(!file_exists($filename)){ die("文件不存在"); } //判断文件是否存在 $fp =fopen($filename,"r"); //打开文件 $file_size=filesize($filename); //声明文件大小 header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); //按字节大小返回 header("Accept-Length:".$file_size); //告诉浏览器文件大小 header("Content-Disposition: attachment; filename=".$filename); //下载框中文件的名字 $buffer=1024; while(!feof($fp)){ $data=fread($fp,$buffer); } //判断文件是否下载完 fclose($fp); //关闭文件 ?>
Visit and verify:
Related recommendations:
Methods for php file permissions when executing under Linux What?
The above is the detailed content of How to use PHP to implement the download function. For more information, please follow other related articles on the PHP Chinese website!