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

    Laravel-文件上传

    标签(空格分隔): php


    介绍

    Laravel 基于 Frank de Jonge 开发的 PHP 包 Flysystem 提供了强大的文件系统抽象。Laravel 文件系统集成对使用驱动处理本地文件系统进行了简化,这些驱动包括Amazon S3,以及 Rackspace 云存储。此外在这些存储选项间切换非常简单,因为对不同系统而言,API 是一致的。
    
    在web应用中,最常见的存储文件案例就是存储用户上传的文件,如用户头像、照片和文档等。Laravel通过使用上传文件实例上的store方法让存储上传文件变得简单。你只需要传入上传文件保存的路径并调用store方法即可
    

    配置

    修改config/filesystems.php
    
    <?php
    

    return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */
    
    'default' => env('FILESYSTEM_DRIVER', 'local'),
    
    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */
    
    'cloud' => env('FILESYSTEM_CLOUD', 's3'),
    
    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "s3", "rackspace"
    |
    */
    
    'disks' => [
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
    
        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
        
        |-----------------------------------------------------------------------------------|
        # 增加配置 [路径可以自定义 storage_path('app/upload') 对应的是 /storage/app/upload ]
        # 如果想修改的话  public_path('/uploads') 对应的是 /public下的uploads
        
        'upload' => [
            'driver' => 'local',
            'root' => storage_path('app/upload'),
        ],
        
        |-----------------------------------------------------------------------------------|
    
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],
    
    ],
    

    ];

    实现

    # 第一种方式
    $fileName = $request->file('image')->store('upload');
    
    file('image') => form表单的name值 
    store('upload') => 刚刚增加的配置
    $fileName => 返回的是文件名 
    
    -----------------------------------------------------------------------------------
    
    # 第二种方式
    $file = $request->file('image');
    # 验证是否上传成功
    if ($file->isValid()) {
        # 原文件名
        $originalName = $file->getClientOriginalName();
        # 扩展名
        $ext = $file->getClientOriginalExtension();
        # Mimetype
        $type = $file->getClientMimeType();
        # 临时绝对路径
        $realPath = $file->getRealPath();
    
        # 自定义文件名
        $fileName = date('Ymd').'/'.uniqid().'.'.$ext;
    
        # 选择磁盘
        $bool = Storage::disk('upload')->put($fileName, file_get_contents($realPath));
        dd($bool);
    }
  • 相关阅读:
    HIDS逐渐的成为主流 java程序员
    怎样做反向域名解析(反向DNS解析)? java程序员
    入侵检测系统的性能的辨别(2) java程序员
    Codeforces Round #146 (Div. 2)
    usaco1.34Prime Cryptarithm
    poj3667 hotel(线段树区间合并)
    poj1330Nearest Common Ancestors(水LCA)
    hdu4135Coprime(容斥原理)
    hdu1541Stars(树状数组)
    usaco 1.43Arithmetic Progressions
  • 原文地址:https://www.cnblogs.com/yanweifeng/p/9505306.html
Copyright © 2011-2022 走看看