A5下载 - 努力做内容最丰富最安全的下载站!

A5站长下载站

当前位置:A5下载 > 源码技巧 > 父类数据 > php多文件上传和下载实现方法

php多文件上传和下载实现方法

时间:2014-02-21 16:34作者:qazwsxzxc人气:156

 文章介绍了用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);

常用php源码下载

 

标签php多文件上传和下载实现方法

相关下载

查看所有评论+

网友评论

网友
您的评论需要经过审核才能显示

公众号