• [php] 文件上传的一个项目emmm


    在这里插入图片描述
    项目完整地址

    <!DOCTYPE html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <title>上传文件</title>
        <link href="./css/bootstrap.min.css" rel="stylesheet">
        <style>
            @font-face {
                font-family: 'JetBrains Mono';
                src: url('font/JetBrainsMono-Bold.ttf') format('truetype');
                font-weight: bold;
                font-style: normal;
            }
    
            body {
                background: linear-gradient(90deg, #83a4d4, #b6fbff);
                font-family: 'JetBrains Mono', Arial, sans-serif;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
    
            .container {
                background-color: #fff;
                padding: 50px;
                border-radius: 10px;
                box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
                width: 50%;
                min-width: 300px;
            }
    
            .btn-primary {
                background-color: #007BFF;
                border: none;
                padding: 10px 20px;
                border-radius: 5px;
                cursor: pointer;
                transition: background-color 0.3s ease;
            }
    
            .btn-primary:hover {
                background-color: #0056b3;
            }
    
            .copyright {
                position: fixed;
                bottom: 0;
                width: 100%;
                text-align: center;
                padding: 15px 0;
                background-color: rgba(0, 0, 0, 0.6);
                color: #ffffff;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <h2 class="mb-4">上传文件</h2>
        <form action="index.php" method="post" enctype="multipart/form-data" class="mb-4">
            <div class="custom-file mb-3">
                <input type="file" class="custom-file-input" name="fileToUpload" id="fileToUpload">
                <label class="custom-file-label" for="fileToUpload">选择文件</label>
            </div>
            <input class="btn btn-primary" type="submit" value="上传文件" name="submit">
        </form>
        <?php
            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                $target_dir = "uploads/"; // 文件保存目录
                $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
                $uploadOk = 1;
                $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    
                // 检查文件类型是否是xxx
                if ($fileType !== "xxx") {
                    echo "抱歉,只允许上传xxx文件.";
                    $uploadOk = 0;
                }
    
                // 检查文件大小
                if ($_FILES["fileToUpload"]["size"] > 20 * 1024 * 1024) {
                    echo "抱歉,你的文件过大.";
                    $uploadOk = 0;
                }
    
                // 如果检查都没有问题,则尝试上传文件
                if ($uploadOk == 1) {
                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                        echo "文件 ". basename($_FILES["fileToUpload"]["name"]). " 已经被上传.";
                    } else {
                        echo "上传文件时出错.";
                    }
                }
            }
        ?>
    </div>
    <div class="copyright">
        &copy; 2023 <font size=5><b><a href="https://zxw-nudt.blog.csdn.net/" style="color: #fff;">tan91</a></b></font>. All rights reserved.
    </div>
    <script src="./js/jquery-3.5.1.slim.min.js"></script>
    <script src="./js/popper.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
    <script>
        // 使选择的文件名显示在标签上
        $('.custom-file-input').on('change', function() {
            let fileName = $(this).val().split('\\').pop();
            $(this).next('.custom-file-label').addClass("selected").html(fileName);
        });
    </script>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
  • 相关阅读:
    含泪整理Redis相关面试题大全
    java-net-php-python-jspm酒店信息管理系统计算机毕业设计程序
    后端连接数据库但是不能读取数据
    Linux——线程详解(一)
    在Windows上使用.NET部署到Docker 《让孩子们走出大坑》
    数据库升级到19后,大量的 ORA-3137「kpoal8Check-5」 错误
    想了解软件测试生命周期知识吗?
    Anchor-free目标检测综述 -- Dense Prediction篇
    猴子也能学会的jQuery第十二期——jQuery遍历(中)
    橘子学JVM之命令行监控06之jcmd
  • 原文地址:https://blog.csdn.net/ZXW_NUDT/article/details/132843949