#yyds干货盘点#laravel 上传文件到七牛云qiniu-laravel-storage

2022年01月15日 阅读数:3
这篇文章主要向大家介绍#yyds干货盘点#laravel 上传文件到七牛云qiniu-laravel-storage,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

1:使用以前,先经过Composer安装:

composer require zgldh/qiniu-laravel-storage

若是执行过程当中报如下错误:说明php没有开启扩展fileinfo,在php扩展开启fileinfo便可php

Your requirements could not be resolved to an installable set of packages.

Problem 1
- stevenyangecho/laravel-u-editor v1.4.2 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- stevenyangecho/laravel-u-editor v1.4.1 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- stevenyangecho/laravel-u-editor v1.4.0 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- Installation request for stevenyangecho/laravel-u-editor ~1.4 -> satisfiable by stevenyangecho/laravel-u-editor[v1.4.0, v1.4.1, v1.4.2].

To enable extensions, verify that they are enabled in your .ini files:
- F:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.

2:而后在 config/app.php providers 中注册服务提供者:

zgldh\QiniuStorage\QiniuFilesystemServiceProvider::classhtml

3:接下来在config/filesystems.php里的disks中新增以下选项:

'disks' => [
... ,
'qiniu' => [
'driver' => 'qiniu',
'domains' => [
'default' => 'xxxxx', //你的七牛域名
'https' => 'xxxxx',//你的HTTPS域名
'custom' => 'xxxxx',//你的自定义域名
],
'access_key'=> 'IKYPvIRNQRxahzSzQh-9nf0er--4oTCzkeIWQM4X', //AccessKey
'secret_key'=> '3KfIusCaWrJGiJnR1kvX6y3UJ1CyBbDHVK7Nm1xi', //SecretKey
'bucket' => '1805a', //Bucket名字
'notify_url'=> 'http://www.zb.cn/lx', //持久化处理回调地址
],
],

 OK,扩展包的安装就暂时介绍到这里,接下来咱们要去七牛注册一个帐号而且将上面的配置完善。前端

4:七牛帐号注册及配置

先去七牛注册一个帐号,点击官网的注册会让咱们选择用户类型,这里我就选择我的用户,接下来按照流程来进项注册就OK了.laravel

点击秘钥管理,就可与看到我的七牛的秘钥了:json

5:七牛在Laravel中的配置

上面已经介绍相关的配置在哪儿,如今咱们要将这些配置在Laravel中使用:app

先在 resources\views 下新建 image.blade.php 视图composer

<!DOCTYPE html>
<html>
<head>
<title>上传图片</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">上传图片</button>
</form>
</body>
</html>

实现上传方法:控制器dom

<?php

namespace App\Http\Controllers;

use zgldh\QiniuStorage\QiniuStorage; //调用基类
use Illuminate\Http\Request;
//七牛云图片上传
class lxController extends Controller
{
public function index(Request $request){
if ($request->isMethod('post')) {
// 判断是否有文件上传
if ($request->hasFile('file')) {
// 获取文件,file对应的是前端表单上传input的name
$file = $request->file('file');
// Laravel5.3中多了一个写法
// $file = $request->file;

// 初始化
$disk = QiniuStorage::disk('qiniu');
// 重命名文件
$fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();

// 上传到七牛
$bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath()));
// 判断是否上传成功
if ($bool) {
$path = $disk->downloadUrl('iwanli/image_'.$fileName);
return '上传成功,图片url:'.$path;
}
return '上传失败';
}
return '没有文件';

}else{
return view('lx.image');
}
}
}

OK,刷新页面就能看到上传后的url地址了。这里只是演示一个最简单的实例,路由定义、视图样式、及逻辑层处理你们按照本身的项目的需求便可。ide

Buy me a cup of coffee :)

以为对你有帮助,就给我打赏吧,谢谢!post

 Buy me a cup of coffee :)​

 www.owenzhang.com/wechat_rewa…​