Laravel 学习笔记之文件上传

自定义添加磁盘——upload

  位置:config/filesystems.php 

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'upload' => [
            'driver' => 'local',  //磁盘驱动
            'root' => storage_path('app/upload'),  //磁盘位置,storage_path指相对于 storage 文件目录
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-bucket',
        ],

    ],

 

控制器中相应操作 

$file = $request->file('source');//获取上传文件相关信息
//判断文件是否上传成功
 if($file->isValid()){
     $origina_name = $file->getClientOriginalName(); //源文件名
     $ext = $file->getClientOriginalExtension(); //源文件扩展名
     $type = $file->getClientMimeType(); //源文件类型
     $file_temp = $file->getRealPath(); //源文件的临时文件绝对路径
     $file_name = date('Ymd',time()).uniqid().'.'.$ext;  //重命名
     $bool = Storage::disk('upload')->put($file_name,file_get_contents($file_temp));

}

  

posted @ 2017-09-01 16:43  骑着蜗牛闯世界  阅读(364)  评论(0编辑  收藏  举报