分类分类
更新时间:2026-10-06 14:25:46作者:qazwsxzxc
文章介绍了用php实现多文件上传和下载的实现方法,希望可以帮助到有所需求的朋友!
核心思路:
header("content-type:application/octet-stream");
header("content-disposition:attachment;filename={$imgfile}");
header("content-length:{$imgsize}");
readfile($path);
index_uploads.php文件代码:
<html>
<head>
<meta charset="utf-8">
<title>index_uploads</title>
</head>
<body>
<form action="uploads.php" method="post" enctype="multipart/form-data">
<input type="file" name="file[]">
<br>
<input type="file" name="file[]">
<br>
<input type="file" name="file[]">
<br>
<input type="file" name="file[]">
<br>
<input type="file" name="file[]">
<br>
<input type="submit" value="uploads">
</form>
</body>
</html>
uploads.php文件代码:
<?php
echo "<pre>";
print_r($_FILES);
echo "</pre>";
$count = count($_FILES['file']['name']);
for ($i = 0; $i < $count; $i++) {
$tmpfile = $_FILES['file']['tmp_name'][$i];
$filefix = array_pop(explode(".", $_FILES['file']['name'][$i]));
$dstfile = "uploads/files/".time()."_".mt_rand().".".$filefix;
if (move_uploaded_file($tmpfile, $dstfile)) {
echo "<script>alert('succeed!');window.location.href='listdir.php';</script>";
} else {
echo "<script>alert('fail!');window.location.href='index_uploads.php';</script>";
}
}
listdir.php文件代码:
<?php
header("content-type:text/html;charset=utf-8");
$dirname = "uploads/files";
function listdir($dirname) {
$ds = opendir($dirname);
while ($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if ($file != '.' && $file != '..'){
if (is_dir($path)) {
listdir($path);
} else {
echo "<tr>";
echo "<td><img src='$path'></td>";
echo "<td><a href='download.php?imgfile=$file'>Download</a></td>";
echo "</tr>";
}
}
}
}
echo "<h2>图片下载|<a href='index_uploads.php'>图片上传</a></h2>";
echo "<table width='700px' border='1px'>";
listdir($dirname);
echo "</table>";
download.php文件代码:
<?php
$imgfile = $_GET['imgfile'];
$path = '.https://file.a5xiazai.com/uploads/files/'.$imgfile;
$imgsize = filesize($path);
header("content-type:application/octet-stream");
header("content-disposition:attachment;filename={$imgfile}");
header("content-length:{$imgsize}");
readfile($path);
相关
摄像头远程监控看家2026官方最新版本生活服务36.4 MBv1.0.02026-10-06
下载公务员省考最新手机版学习办公57.5 MBv6.09.112026-10-06
下载野性蜂巢(Wild Hive)经营养成267.49 Mv1.66.42026-10-04
下载趣好卖app网上购物74.76 Mv5.2.52026-10-04
下载兔叽月记app医疗健康55.33 Mv1.5.22026-10-04
下载魔力弹神策略游戏33.56 Mv1.0.02026-10-04
下载大破坏幸存者策略游戏62.28 Mv1.0.32026-10-04
下载啃豆小仓拼豆app实用工具41.74 Mv1.32026-10-04
下载方块公国安卓移植版策略游戏1.58Gv1.2.202026-10-04
下载快车顺风车出行2026官方最新版本生活服务82.0 MBv1.1.22026-10-04
下载网约上门按摩2026官方最新版本生活服务73.9 MBv2.0.22026-10-04
下载道运考试通(道路考练工具)学习办公32.8 MBv6.0.12026-10-04
下载










