laravel进阶--4 文件上传

33 篇文章 0 订阅

laravel 上传 php 需要开启 fileinfo 扩展

前端页面

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">上传文件</div>

                <div class="panel-body">
                    <form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
                        {{ csrf_field() }}

                        <div class="form-group">
                            <label for="file" class="col-md-4 control-label">选择文件</label>

                            <div class="col-md-6">
                                <input id="file" type="file"  name="file" >

                            </div>
                        </div>

      

                        <div class="form-group">
                            <div class="col-md-8 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    上传
                                </button>

                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

路由

Route::any('upload', 'StudentController@upload');

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class StudentController extends Controller
{
    //
    public function upload(Request $request){
        if($request->isMethod('POST')){
           // var_dump($_FILES);
            $file = $request->file('file');

            if(!$file->isValid()){

                //失败提示
                return redirect('upload');
            }

            //方法一 md5生成
            // $path = $request->file('file')->store('public');
            // var_dump(env('APP_URL').'/'.$path);exit();

            $path = $request->file('file')->store('/public/'.date('Y-m-d').'/avatars');
            $avatar = Storage::url($path);
            //不可以显示
            // "http://tt001.com/public/2019-08-29/avatars/3A1sUBaCeyQF0OQ7eRgTwfAAaXgW87EqgpbEPZVZ.jpeg" 
            var_dump(env('APP_URL').'/'.$path);

            //可以显示
            // "http://tt001.com/storage/2019-08-29/avatars/3A1sUBaCeyQF0OQ7eRgTwfAAaXgW87EqgpbEPZVZ.jpeg"
            var_dump(env('APP_URL').$avatar);
            exit();
            //方法二md5生成
            // $path = Storage::putFile('avatar', $request->file('file'));
            // var_dump($path);exit();

            //方法三 指定文件名
            // $path = $request->file('file')->storeAs(
            //     'avatars', 'newFileName'
            // );
            // var_dump($path);exit();
            
            //方法四 指定磁盘 指定路径 文件名随机
            $path = $request->file('file')->store(
                'avatars', 'public'
            );
            
            var_dump($path);exit();

            //方法五
            // 原文件名  20181207140247195.jpg
            $originalName = $file->getClientOriginalName();
            // 扩展名  jpg
            $ext = $file->getClientOriginalExtension();
            // MimeType  image/jpeg
            $type = $file->getClientMimeType();
            // 临时绝对路径  C:\Windows\phpFBC1.tmp
            $realPath = $file->getRealPath();

            $filename = date("ymdhis").'-'.uniqid().'.'.$ext;
            $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
           
            var_dump($bool);
           exit;
            return redirect('upload');
           
        }
        
       //http://tt001.com/storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg
        echo asset('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');
        echo "<br />";
        //D:\phpStudy\PHPTutorial\WWW\blog88\storage\storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg 
        echo storage_path('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');
        return view('student.upload');
    }
}

添加磁盘

<?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',
        ],

        'uploads' => [
            'driver' => 'local',
            // 'root' => storage_path('app/uploads'),
            'root' => public_path('uploads'),
            
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

    ],

];

文件 URL

 

使用 local 或 s3 驱动时,可以使用 url 方法获取给定文件的 URI。如果你使用的是 local 驱动,通常会在给定路径前加上 /storage,并返回该文件的相对 URL;如果使用的是 s3 或 rackspace 驱动,则会返回完整的远程 URL:

use Illuminate\Support\Facades\Storage; 
$url = Storage::url('file1.jpg');

 

注:记住,如果你在使用 local 驱动,所有需要公开访问的文件都应该存放在 storage/app/public 目录下,此外,你还需要创建一个指向 storage/app/public 目录的软链接 public/storage。

echo asset('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');

http://tt001.com/storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于 Laravel进阶课程和资料,我可以向您推荐一些资源。 1. 官方文档:Laravel 的官方文档是学习和深入了解 Laravel 的最好资源之一。您可以在 Laravel 官方网站(https://laravel.com/docs)上找到最新的文档,并从基础到高级的各个方面进行学习。 2. Laravel 学院(Laracasts):Laravel 学院是一个非常受欢迎的 Laravel 视频教程网站。它提供了大量的 Laravel 进阶课程,涵盖了许多主题,包括数据库、认证、缓存、队列等等。您可以通过订阅他们的服务(https://laracasts.com/)来获得无限访问权限。 3. Laravel News:Laravel News 是一个社区驱动的 Laravel 资讯网站,提供了有关 Laravel 的最新消息、教程和工具等内容。您可以参考他们的网站(https://laravel-news.com/)以获取更多关于 Laravel进阶资料。 4. Laravel 扩展包:Laravel 生态系统中有很多优秀的扩展包,通过使用这些扩展包可以提高您的开发效率和功能。您可以通过 Laravel 官方网站的扩展包页面(https://packagist.org/)来查找并了解这些扩展包。 5. Laravel 论坛:Laravel 论坛是一个讨论 Laravel 相关话题的社区,您可以在这里与其他 Laravel 开发者交流和分享经验。Laravel China(https://learnku.com/laravel)是一个中文的 Laravel 论坛,您可以在这里寻求帮助、提问问题以及获取进阶资料。 希望这些资源能帮助到您的学习和进阶!如果您有任何其他问题,请随时告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值