首页 > 代码库 > PHP文件下载功能实现

PHP文件下载功能实现

直接贴代码!

这是test_download.html,由用户输入文件名,再点击下载,传参给download.php文件

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>测试下载</title>
</head>
<body>
	<form action="download.php" method="post">
		请输入文件名:<input type="text" name="filename">
		<input type="submit" name="submit" value="http://www.mamicode.com/点击下载">
	</form>
</body>
</html>

 

这是download.php,大部分内容都是header(),差不多包含常用的文件类型了

<?php 
	if($_POST[‘submit‘]){
		$filename=$_POST[‘filename‘];
	}
	header("Content-Type:text/html;charset=utf-8");
	header("Content-Disposition:attachment;filename=".$filename);
	header("Content-Type: image/jpg");
	header("Content-Type:image/png");
	header("Content-Type:image/gif");
	header("Content-Type:image/jpeg");
	header("Content-Type:image/x-png");
	header("Content-Type:text/txt");
	header("Content-Type:text/plain");
	header("Content-Type:text/html");
	header("Content-Type:audio/basic");
	header("Content-Type:audio/x-m4a");
	header("Content-Type:video/mpeg");
	header("Content-Type:application/zip");
	header("Content-Type:application/x-tar");
	header("Content-Type:application/x-gz");
	header("Content-Type:application/msword");
	header("Content-Type:application/powerpoint");
	header("Content-Type:application/vnd.ms-excel");
	header("Content-Type:application/x-msdownload");
	readfile($filename);
 ?>

 

这只是一个示例,至于怎么用,可以自己修改的哈

PHP文件下载功能实现