码迷,mamicode.com
首页 > Web开发 > 详细

php-tp5文件上传

时间:2018-08-16 19:40:04      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:ext   public   文件上传   空格   jpg   tab   conf   info   inf   

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

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()];
        }
    }

}

 

php-tp5文件上传

标签:ext   public   文件上传   空格   jpg   tab   conf   info   inf   

原文地址:https://www.cnblogs.com/930115586qq/p/9488898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!