文件下载:
1.设置文件mime类型
header(“content-type:image/png”);
2.设置文件名和内容类型
header(“content-disposition:attachment;filename=a.txt”);
3.设置文件大小
header(“content-length:30KB”);
4.下载文件
readfile(‘a.txt’);
index.php
<?php
// <!-- 获取filesh中的文件 -->
$dir='files';
$files=scandir($dir);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件下载</title>
</head>
<body>
<h3> 文件下载:</h3>
<hr>
<?php
foreach($files as $file){
if($file!='.' && $file!='..'){
$filepath=$dir.'/'.$file;
echo "<p>{$file} <a href='down.php?filepath={$file}'>下载</a></p>";
}
}
?>
</body>
</html>
down.php
<?php
$file=$_GET['filepath'];
$filepath='files/'.$file;
echo $filepath;
$size=filesize(($filepath));
//文件下载
// 1.设置文件mime类型
header("content-type:application/octet-stream");
// 2.设置文件名和内容类型
header("content-disposition:attachment;filename={$file}");
// 3.设置文件大小
header("content-length:{$size}");
// 4.下载文件
readfile($filepath);
?>
**
**
**
**
**
**