• php文件上传(视频图片或者其他)


    html页面

    <html>
    
    <head>
    
        <meta charset="utf-8">
    
        <title></title>
    
    </head>
    
    <body>
    
    <form action='index.php' method=post enctype="multipart/form-data">
        
        <!-- 上传文件限制,会在传递至index.php之前先执行验证文件大小,value为上传文件的最大值 ,单位为b,600000为600kb-->
        <input type="hidden" name="MAX_FILE_SIZE" value="600000">
        
        <input type="file" name="upfile">
    
        <input type="submit" value='上传文件'>
    
    </form>
    
    </body>
    
    </html>

    php

    <?php
    header("Content-type:text/html;charset=UTF-8");
    /**
    
     * PHP上传视频
    
     */
    
    $upfile = $_FILES['upfile'];
    
     
    
    function upload_file($files, $path = "./upload",$imagesExt=['jpg','png','jpeg','gif','mp4'])
    {
        
        // 判断错误号
        if ($files['error'] == 00) {
    
            
            $ext = strtolower(pathinfo($files['name'],PATHINFO_EXTENSION));
            // 判断文件类型
            if (!in_array($ext,$imagesExt)){
    
                return "非法文件类型";
    
            }
    
            // 判断是否存在上传到的目录
    
            if (!is_dir($path)){
    
                mkdir($path,0777,true);
    
            }
    
            // 生成唯一的文件名
    
            $fileName = md5(uniqid(microtime(true),true)).'.'.$ext;
    
            // 将文件名拼接到指定的目录下
    
            $destName = $path."/".$fileName;
    
            // 进行文件移动
    
            if (!move_uploaded_file($files['tmp_name'],$destName)){
    
                return "文件上传失败!";
    
            }
    
            return "文件上传成功!";
    
        } else {
    
            // 根据错误号返回提示信息
    
            switch ($files['error']) {
    
                case 1:
    
                    echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
    
                    break;
    
                case 2:
    
                    echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
    
                    break;
    
                case 3:
    
                    echo "文件只有部分被上传";
    
                    break;
    
                case 4:
    
                    echo "没有文件被上传";
    
                    break;
    
                case 6:
              echo "找不到临时文件夹";
              break; case 7: echo "系统错误"; break; } } } var_dump(upload_file($upfile)); ?>
  • 相关阅读:
    idea打开service窗口
    windows使用命令行终止端口的进程
    struts2
    struts_spring_hibernate
    struts
    pycharm自动提示documentation (auto Ctrl+Q)
    C语言结构体初始化的几种方法
    github sshkey生成踩坑
    修复Windows11搜索框卡死问题/关闭Windows 11网络搜索
    fastjson的字段
  • 原文地址:https://www.cnblogs.com/junyi-bk/p/11014318.html
Copyright © 2020-2023  润新知