zoukankan      html  css  js  c++  java
  • php-tp5文件上传

    文件上传没有上传上会出现的问题

    1.没有写enctype="multipart/form-data"

    2.名字是type='file' 的name  $file = request()->file('filename');

    3.我总忽略的一点 文件的大小是否超过了php.ini upload_max_filesize的最大的大小 一般默认是2M  post_max_size post方法的最大大小

    4.有一些人会遇见单双引号的问题 request()->file('filename'); 里面要双引  应该是用了中文的空格 

    tp5文件上传限制大小格式的写法

            

    public function handle($img = 'files')
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($img);
        // 移动到框架应用根目录/public/uploads/ 目录下

        if ($file) {
            $new_file =  'uploads' . '/' . USER_ID . '/' . date('Ym') . '/' . date('d') . '/';
            //return $new_file;
            $bef_info = $file->getInfo();

            if (!file_exists(ROOT_PATH .'public' . '/' . $new_file)) {
                //检查是否有该文件夹,如果没有就创建,并给予最高权限
                mkdir(ROOT_PATH .'public' . '/' . $new_file, 0777, true);
            }

            $validata = ['size' => 1024000, 'ext' => 'jpg,png,gif,txt,doc,hlp,wps,rtf,html,pdf,docx,rar,zip,arj,gz,z,waz,aif,au,mp3,ram,wma,mmf,amr,aac,flac,xlsx'];
            $info = $file->rule('uniqid')->validate($validata)->move(ROOT_PATH . $new_file);

            if ($info) {
                // 成功上传后 获取上传信息

                $file_info = array(
                    "image-type" => $info->getExtension(),
                    "image-frames" => 1,
                    "image-height" => 377,
                    "sign" => "65096b4ff9d0a1c3b41c343b8fa27605",
                    "code" => 200,
                    "file_size" => 75761,
                    "image-width" => 547,
                    "url" => $new_file . $info->getSaveName(),
                    "time" => 1508835353,
                    "message" => "ok",
                    "mimetype" => "image/jpeg",
                    "name" => $info->getFilename(),
                    "path" => $new_file,
                    'bet_info' => $bef_info

                );

                $data = array(
                    "type" => $info->getExtension(),
                    //"image-frames"=>1,
                    //"image-height"=>377,
                    //"image-width"=>547,
                    //"sign"=>"65096b4ff9d0a1c3b41c343b8fa27605",
                    //"code"=>200,
                    "size" => $bef_info["size"],
                    "url" => $new_file . $info->getSaveName(),
                    //"time"=>1508835353,
                    //"message"=>"ok",
                    "savename" => $info->getSaveName(),
                    "name" => $bef_info["name"],
                    "mimetype" => $bef_info["type"],
                );
                //print_r($data);


                // Db::connect(DB_CONFIG)->table('rrs_upload_files')->insert($data);


                return ['code' => 0, 'file_info' => $file_info,'data'=>$data];

                // 输出 jpg
                //echo $info->getExtension()." ";
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getSaveName()." ";
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getFilename()." ";
            } else {
                // 上传失败获取错误信息
                return ['code' => 1, 'data' => $file->getError()];
            }
        }

    }

  • 相关阅读:
    Python入门示例系列07 Python注释
    Python入门示例系列09 Python算术运算
    Python入门示例系列10 字符串(初级)
    SQL循环26个字母插入到一个表中
    UBUNTU LINUX中连接ANDROID 小米真机调试
    Ubuntu下包含2种远程桌面的方式:VINOServer以及VNC Server
    一个合格的程序员应该读过哪些书
    linux下如何查看文件编码格式及转换文件编码
    尽量不要使用文本模式
    S.O.L.I.D.类设计原则
  • 原文地址:https://www.cnblogs.com/930115586qq/p/9488898.html
Copyright © 2011-2022 走看看