php怎么实现多文件上传和下载(php,开发技术)

时间:2024-05-03 18:43:12 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    php%E6%80%8E%E4%B9%88%E5%AE%9E%E7%8E%B0%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%92%8C%E4%B8%8B%E8%BD%BD

复制代码 代码如下:


<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中获取参数下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18