• php文件上传相关知识点回顾


    近来正在回顾PHP的文件上传。在此做个记录。

    
    <?php
    date_default_timezone_set('PRC');
    if(isset($_POST['submit'])) {
        echo "<pre>";
        var_dump($_FILES['file']);
        echo "</pre>";
    
        //获取文件后缀名方法一:
        //将文件名以“.”为界限分割为数组
        //end函数对于数组,取数组中的最后一个元素
        $ext = end(explode(".", $_FILES['file']['name']));
    
        //获取文件后缀名方法二:
        //找出文件名中的点所在位置,截取此位置后的字符串
        //$extpos = strrpos($_FILES['file']['name'],'.');
        //$ext = substr($_FILES['file']['name'], $extpos+1);
        //echo "文件后缀名为 :" . $ext;
    
        //设置可上传的文件类型
        $allowType = [
            "gif", "jpeg", "jpg", "png"
        ];
        //检测上传文件是否支持
        if (!in_array($ext, $allowType)) {
            die("请选择正确的文件格式上传");
        } else {
            //以年月日时分秒命名上传文件
            $filename = date("YmdHis", time()) . ".".$ext;
            //以年-月-日命名上传文件夹,保存同一日上传的文件
            $directory = "uploads"."/".date("Y-m-d", time());
            //检测目标文件目录是否存在
            if (!is_dir($directory)) {
                //不存在则创建文件夹
                mkdir ($directory,0777,true);
                //将临时文件保存到当天目录下
                move_uploaded_file($_FILES['file']['tmp_name'], $directory . "/" . $filename);
            } else {
                //检测目标文件夹中是否已存在该文件
                if (!file_exists()) {
                    //不存在则将临时文件保存到指定目录下
                    move_uploaded_file($_FILES['file']['tmp_name'], $directory . "/" . $filename);
                }else{
                    die("该文件已经存在");
                }
            }
        }
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>文件上传作业</title>
        <style type="text/css">
            table{border:0;cellspacing: 0;cellpadding:0}
            table tr td{text-align: center;height: 25px;line-height: 25px; 200px; border: 1px solid darkolivegreen;}
        </style>
    </head>
    <body>
    <form action="homework.php" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td>请选择文件</td>
                <td><input type="file" name="file"></td>
                <td><input type="submit" name="submit"></td>
            </tr>
        </table>
    </form>
    </body>
    </html>
    

    原文地址:https://segmentfault.com/a/1190000016359542

  • 相关阅读:
    一种线程安全的handle
    基于数组的无锁队列(译)
    distri.lua的web运维工具
    distri.lua重写开源手游服务器框架Survive
    99 Lisp Problems 二叉树(P54~P69)
    99 Lisp Problems 列表处理(P1~P28)
    TSPL学习笔记(4):数组相关练习
    TSPL学习笔记(3):排序算法练习
    用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 热更新Live2D
    用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用FairyGUI (二)
  • 原文地址:https://www.cnblogs.com/lalalagq/p/9971429.html
Copyright © 2020-2023  润新知