阿里云新用户优惠

实例 简单文件管理系统

一、项目介绍

1、实例图

2、代码目录结构
  • index.php 项目首页
  • footer.php 项目页脚
  • upload.php 文件上传处理页
  • search.php 查询页面
  • showFile.php 结果显示页
  • download.php 文件下载处理页
  • uploads 上传文件存储目录
3、相关函数

二、页面代码

1、 index.php 项目首页
<?php
    session_start();
    function create_code($len = 4)
    {
        $seed = "ABCDE12345";
        return substr(str_shuffle($seed), 0, $len);;
    }
    $code = create_code();
    $_SESSION["user_code"] = $code; 
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件管理系统</title>
</head>
<body>
    <form action="./upload.php" method="post" enctype="multipart/form-data">
        <div>
            姓名:<input type="text" name="xm">
        </div>
        <div>
            学号:<input type="text" name="xh">
        </div>
        <div>
            选择文件:<input type="file" name="upfile">            
        </div>
        <p>
            验证码:<?php echo $code; ?><input type="text" name="code" style="margin-left: 15px;" placeholder="请输入验证码">
        </p>
        <input type="submit" value="提交">
    </form>
    <div style="margin-top: 20px;">
        <a href="search.php">查询记录</a>
    </div>
    <hr>
</body>
<style>
    form{
        width: 30%;
    }
    form > div {
        padding: 15px 0;
    }
    form > div > input{
        width: 60%;
    }
</style>
</html>
<?php
    include_once("./footer.php");
?>
2、 footer.php 项目页脚
<?php
    echo "<p>Copyright©2021-".date("Y")."荆州理工职业学院</p>";
?>
3、upload.php 文件上传处理页
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件管理系统</title>
</head>
<style>
    .error {
        color: red;
        margin-top: 15px;
    }
</style>
<body>
    <a href="/"><<返回上传界面</a>
    <br>
    <?php
        if(!isset($_POST["code"])){
            echo '<p class="error">请输入验证码<p>';
            return;
        }
        if(isset($_SESSION["user_code"]) && $_SESSION["user_code"] == $_POST["code"]){
            unset($_SESSION["user_code"]);
        } else {
            echo '<p class="error">验证码不正确<p>';
            return;
        }
        if(!empty($_FILES) && $_FILES['upfile']['tmp_name'] != ''){
            if(empty($_POST['xm'])){
                echo '<p class="error">姓名必须填写<p>';
                return;
            }
            if(empty($_POST['xh'])){
                echo '<p class="error">学号必须填写<p>';
                return;
            }
            $xm = trim($_POST['xm']);
            $xh = trim($_POST['xh']);
            $tmpname   = $_FILES['upfile']['tmp_name'];     // 临时文件名称
            $name      = $_FILES['upfile']['name'];         // 文件的原名称
            $path      = './uploads/'.$xm.'_'.$xh;// 上传目录
            $file_name = date('YmdHis').rand(100,999)."_".$name;// 避免文件重名,更改文件名称
            if(!file_exists($path)){
                mkdir($path, 0777, true);
            }
            if(move_uploaded_file($tmpname, $path.'/'.$file_name)){
                echo $name." 上传成功!<br>";
                echo "<a href='showFile.php?xm={$xm}&xh={$xh}'>点击查看上传文件</a>";
            }else{
                echo $name." 上传失败!";
            }
        }
        include_once("./footer.php");
    ?>
</body>
</html>
4、search.php 查询页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件管理系统</title>
</head>
<body>
    <a href="/"><<返回上传界面</a>
    <form action="showFile.php" method="get">
        <div>
            姓名:<input type="text" name="xm">
        </div>
        <div>
            学号:<input type="text" name="xh">
        </div>
        <input type="submit" value="查询">
    </form>
    <hr>
</body>
<style>
    form{
        width: 30%;
    }
    form > div {
        padding: 15px 0;
    }
    form > div > input{
        width: 60%;
    }
    .error {
        color: red;
        margin-top: 15px;
    }
</style>
</html>
<?php
    include_once("./footer.php");
?>
5、showFile.php 结果显示页
<?php
    if(!isset($_GET['xm']) || empty($_GET['xm'])){
        echo "参数错误!";
        return; 
    }
    if(!isset($_GET['xh']) || empty($_GET['xh'])){
        echo "参数错误!";
        return; 
    }
    $xm = trim($_GET['xm']);
    $xh = trim($_GET['xh']);
    $file_name = $xm.'_'.$xh;
    $path = ".".DIRECTORY_SEPARATOR."uploads".DIRECTORY_SEPARATOR.$file_name;
    if(!file_exists($path)){
        echo "未找到文件目录";
        return;
    }
    function readAllFileFromDir($dir, $level = 0)
    {
        if(!is_dir($dir)) return false;
        $dirs = scandir($dir, 1);
        $tempFiles = [];
        foreach($dirs as $d){
            if($d =='.' || $d =='..'){
                continue;
            }
            $file = $dir.DIRECTORY_SEPARATOR.$d;
            if(is_file($file))
            {
                array_push($tempFiles, $file);
            }
            elseif(is_dir($file))
            {
                $tempFiles = array_merge($tempFiles, readAllFileFromDir($file, $level++));
            }
        }
        return $tempFiles;
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件管理系统</title>
    <style>
        .ml15{
            margin-left: 15px;
        }
    </style>
</head>
<body>
    <a href="/"><<返回上传界面</a>
    <div style="margin-top: 20px;">
        <span>姓名:<?php echo $xm; ?></span><span class="ml15">学号:<?php echo $xh; ?></span>
    </div>
    <?php
        $fileinfos = readAllFileFromDir($path);
        $fileCount = 0;
        $fileSizeCount = 0;
        foreach($fileinfos as $name){
            if($name == '.' || $name == '..') continue;
            $fileCount++;
            $fileSizeCount += ceil(filesize($name)/1024);
        }
        echo "<div>";
        echo "<span>文件数量:{$fileCount}个</span>";
        echo "<span class='ml15'>文件大小:{$fileSizeCount}kb</span>";
        echo "</div>";
    ?>
    <hr>
    <table style="text-align: center;">
        <thead>
            <tr>
                <th width="350">文件名</th>
                <th width="120">大小</th>
                <th width="220">创建时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php                
                foreach($fileinfos as $name){
                    if($name == '.' || $name == '..') continue;
                    $downloadname = ltrim($name, ".".DIRECTORY_SEPARATOR.'uploads');
                    $ctime = date("Y-m-d H:i:s", filectime($name));
                    $fsize = ceil(filesize($name)/1024);                    
                    $tempname = ltrim(DIRECTORY_SEPARATOR. str_replace('_', '', mb_strstr(basename($name), '_')), '\\');
                    echo "<tr>";
                    echo "<td width='120'>{$tempname}</td>";
                    echo "<td width='120'>{$fsize}kb</td>";
                    echo "<td>{$ctime}</td>";
                    echo "<td><a href='./download.php?file_name={$downloadname}'>下载</a><td/>";
                    echo "</tr>";
                }
            ?>
        </tbody>
    </table>
</body>
</html>
<?php
    include_once("./footer.php");
?>
6、download.php 文件下载处理页
<?php
    if(!isset($_GET['file_name']) || empty($_GET['file_name'])){
        echo "参数错误!";
        return; 
    }
    $file_name = trim($_GET['file_name']);
    $path = "./uploads/".$file_name;
    if(!file_exists($path)){
        echo "未找到文件";
        return;
    }
    //增加路径验证
    if(strpos(realpath($path), realpath("./uploads")) !== 0){
        echo "路径错误:". dirname($path);
        return;
    }
    $file_Size = filesize($path);
    $downname = str_replace('_', '', mb_strstr(basename($file_name), '_'));

    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");
    header("Accept-Length:".$file_Size);
    header("Content-Disposition: attachment; filename=".$downname);
    readfile($path);

关注微信公众号,与我交流吧~

分享