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

}

 

posted @ 2018-08-16 18:01  噼里啪啦哒哒哒  阅读(2802)  评论(0编辑  收藏  举报