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

Laravel 文件上传

时间:2020-07-24 10:08:13      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:conf   systems   use   tor   csrf   一个   resources   php   content   

1 配置文件

config\filesystems.php

复制一个配置

‘public‘ => [
‘driver‘ => ‘local‘,
‘root‘ => storage_path(‘app/public‘),
‘url‘ => env(‘APP_URL‘).‘/storage‘,
‘visibility‘ => ‘public‘,
],
然后配置成如下形式:
 //storage_path 指storage目录
        ‘uploads‘ => [
            ‘driver‘ => ‘local‘,
            ‘root‘ => storage_path(‘app/uploads‘),

        ],

 2 创建路由

Route::any(‘/upload‘,[‘uses‘=>‘StudentController@upload‘]);

3 模板文件

\resources\views\student\upload.blade.php

@extends(‘layouts.app‘)
@section(‘content‘)
<div>
    <form method="POST" action="" enctype="multipart/form-data">
        @csrf
        <div >
            <input type="file" name="source" id="file" > 请选择文件
            <button type="submit">
               确认上传
            </button>
        </div>
    </form>
</div>
@endsection

4 控制器

\app\Http\Controllers\StudentController.php

public  function  upload(Request $request)
    {
        if ($request->isMethod("POST")){
            //var_dump($_FILES);
            $file = $request->file(‘source‘);
            //判断是否上传成功
            if ($file->isValid()){
                //获取原文件名
                $originalName = $file->getClientOriginalName();
                //获取扩展名
                $ext = $file->getClientOriginalExtension();
                //文件的类型
                $file->getClientMimeType();
                //临时文件的绝对路径
                $realPath = $file->getRealPath();
                //新文件名
                $fileName = date(‘Y-m-d-H-i-s‘).‘-‘.uniqid().‘.‘.$ext;
                $bool = Storage::disk(‘uploads‘)->put($fileName,file_get_contents($realPath));
                if($bool){
                    echo ‘上传成功‘;
                }
            }

        }
        return view(‘student.upload‘);
    }

 

Laravel 文件上传

标签:conf   systems   use   tor   csrf   一个   resources   php   content   

原文地址:https://www.cnblogs.com/polax/p/13369778.html

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