感谢大神的文章供我参考:https://blog.csdn.net/createNo_1/article/details/84290820
一、Composer 安装插件
$ composer require "overtrue/laravel-filesystem-qiniu"
二、config/app.php 中注册服务提供者,和引入配置文件 filesystems.php
// 文件驱动配置文件
// 注册后 在config目录下创建 filesystems.php 文件
$app->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
// 服务提供者
$app->register(Overtrue\LaravelFilesystem\Qiniu\QiniuStorageServiceProvider::class);
三、config/filesystems.php 里的 disks 中新增七牛的文件驱动配置
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. A "local" driver, as well as a variety of cloud
| based drivers are available for your choosing. Just store away!
|
| Supported: "local", "s3", "qiniu"
|
*/
'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.
|
*/
/*
|--------------------------------------------------------------------------
| 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.
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'qiniu' => [
'driver' => 'qiniu',
'access_key'=> env('QINIU_ACCESS_KEY', ''),
'secret_key' => env('QINIU_SECRET_KEY', ''),
'bucket' => env('QINIU_BUCKET'),
'domain' => env('QINIU_DOMAIN', ''),
],
],
];
四、控制器里新建方法 avatarUpload
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
// 上传图片到 七牛云
public function avatarUpload(Request $request){
if ($request->hasFile('file') && $request->file('file')->isValid()) {
// getClientOriginalName 获取图片文件的名字
// getClientOriginalExtension 获取图片的后缀名
$newFileName = md5($request->file('file')->getClientOriginalName() . time())
. '.'
. $request->file('file')->getClientOriginalExtension();
// 2.保存到该磁盘
// 文件名重复就会覆盖=>因此要回到上一步=>自定义文件名 $newFileName
Storage::disk('qiniu')->put($newFileName, File::get($request->file('file')->path()));
// 输出图片地址
echo $avatar = 'http://' . env('QINIU_DOMAIN') . '/'.$newFileName;
}
return [
'code' => 0,
'message' => $request->file('file')->getErrorMessage(),
];
}
五、多图上传
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
// 上传图片到 七牛云
public function avatarUpload(Request $request){
if ($request->hasFile('file') ) {
// getClientOriginalName 获取图片文件的名字
// getClientOriginalExtension 获取图片的后缀名
$imgUrl = array(); // 图片集合
foreach ($request->file('file') as $v) {
$newFileName = md5($v->getClientOriginalName() . time())
. '.'
. $v->getClientOriginalExtension();
// 2.保存到该磁盘
// 文件名重复就会覆盖=>因此要回到上一步=>自定义文件名 $newFileName
Storage::disk('qiniu')->put($newFileName, File::get($v->path()));
// 输出图片地址
$imgUrl[] = 'http://' . env('QINIU_DOMAIN') . '/'.$newFileName;
}
dd( $imgUrl );
}
return [
'code' => 0,
'message' => $request->file('file')->getErrorMessage(),
];
}
网友评论