Home  >  Article  >  Backend Development  >  laravel如何上传文件到七牛云

laravel如何上传文件到七牛云

PHPz
PHPzOriginal
2016-06-06 20:41:052908browse

laravel如何上传文件到七牛云

laravel如何上传文件到七牛云?

1:使用之前,先通过Composer安装:

composer require zgldh/qiniu-laravel-storage

如果执行过程中报以下错误:说明php没有开启扩展fileinfo,在php扩展开启fileinfo即可

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::class

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',  //持久化处理回调地址
        ],
    ],

4: OK,扩展包的安装就暂时介绍到这里,接下来我们要去七牛注册一个账号并且将上面的配置完善。

七牛账号注册及配置

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

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

七牛在Laravel中的配置

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

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

<!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>

 

实现上传方法:控制器

<?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(&#39;post&#39;)) {
     // 判断是否有文件上传
        if ($request->hasFile(&#39;file&#39;)) {
            // 获取文件,file对应的是前端表单上传input的name
            $file = $request->file(&#39;file&#39;);
            // Laravel5.3中多了一个写法
            // $file = $request->file;
 
            // 初始化
            $disk = QiniuStorage::disk(&#39;qiniu&#39;);
            // 重命名文件
            $fileName = md5($file->getClientOriginalName().time().rand()).&#39;.&#39;.$file->getClientOriginalExtension();
 
            // 上传到七牛
            $bool = $disk->put(&#39;iwanli/image_&#39;.$fileName,file_get_contents($file->getRealPath()));
            // 判断是否上传成功
            if ($bool) {
                $path = $disk->downloadUrl(&#39;iwanli/image_&#39;.$fileName);
                return &#39;上传成功,图片url:&#39;.$path; 
            }
            return &#39;上传失败&#39;;
        }
        return &#39;没有文件&#39;;
}else{
return view(&#39;lx.image&#39;);
}
    }
}

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn