您好,登錄后才能下訂單哦!
這篇文章主要講解了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。