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');
        }
  • 相关阅读:
    C#类头部声明样式
    VisualStudio使用技巧及快捷键
    #使用ListView更新数据出现闪烁解决办法
    获取公网IP地址
    JArray数组每个JObject对象添加一个键值对
    部署网站出现System.ServiceModel.Activation.HttpModule错误
    MYSQL存储引擎的比较
    数据库索引原理(转载)
    皮尔逊相关系数
    进程与线程
  • 原文地址:https://www.cnblogs.com/polax/p/13369778.html
Copyright © 2011-2022 走看看