这篇文章主要讲解了“php怎么实现多文件上传和下载”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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>
index_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>";
}
}
uploads.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 "<h3>图片下载|<a href='index_uploads.php'>图片上传</a></h3>";
echo "<table width='700px' border='1px'>";
listdir($dirname);
echo "</table>";
listdir.php
复制代码 代码如下:
<?php
$imgfile = $_GET['imgfile'];
$path = './uploads/files/'.$imgfile;
$imgsize = filesize($path);
header("content-type:application/octet-stream");
header("content-disposition:attachment;filename={$imgfile}");
header("content-length:{$imgsize}");
readfile($path);
download.php
download.php
核心下载:
复制代码 代码如下:
header("content-type:application/octet-stream");
header("content-disposition:attachment;filename={$imgfile}");
header("content-length:{$imgsize}");
readfile($path);
感谢各位的阅读,以上就是“php怎么实现多文件上传和下载”的内容了,经过本文的学习后,相信大家对php怎么实现多文件上传和下载这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。