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');
        }
  • 相关阅读:
    一文解读AI芯片之间的战争 (转)
    一文解读ARM架构 (转)
    一文解读云计算 (转)
    一文解读裸金属云 (转)
    一文解读发布策略 (转)
    C#使用OracleDataReader返回DataTable
    centos8平台上php7.4的生产环境配置
    centos8安装php7.4
    centos8安装java jdk 13
    docker的常用操作之二:docker内无法解析dns之firewalld设置等
  • 原文地址:https://www.cnblogs.com/polax/p/13369778.html
Copyright © 2011-2022 走看看