zoukankan      html  css  js  c++  java
  • Laravel 文件上传

    1 配置文件

    configfilesystems.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 模板文件

    esourcesviewsstudentupload.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 控制器

    appHttpControllersStudentController.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');
        }
  • 相关阅读:
    HTTP状态码
    CEFsharp使用代理及切換
    powershell生成时间戳13和10位
    不用Root卸载手机自带应用
    夜间浏览更护眼
    windows 10 1709精简版安装 Microsoft Store
    在elementary os上安装brave 浏览器
    单语言精简版win10下中文网页无法正常
    为输出添加序号
    SpringBoot对静态资源的映射规则
  • 原文地址:https://www.cnblogs.com/polax/p/13369778.html
Copyright © 2011-2022 走看看