1.准备
七牛云/阿里云OSS
https://github.com/pfinal/storage
2.composer安装包
安装
composer require pfinal/storage
本地存储 Local
阿里云 AliOss
请先 composer require aliyuncs/oss-sdk-php
7牛存诸 Qiniu
请先 composer require qiniu/php-sdk
Ftp
请先 composer league/flysystem
3.引入使用示例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/11/20 * Time: 14:13 */ namespace App\Http\Controllers; use PFinal\Storage\Qiniu; use PFinal\Storage\AliOss; class CacheController { /** * 七牛云 * 注册认证可获得免费的10GB空间 */ public function qiniu() { $config = array( 'accessKey' => 'xxxxxxxxxxxxx', 'secretKey' => 'xxxxxxxxxxxxx', 'bucketName' => 'OSS名称', 'baseUrl' => '绑定域名或者七牛云默认暂时给你使用的域名', 'separator' => '-', ); $qiniu = new Qiniu($config); $bool = $qiniu->put('test/1.jpg', file_get_contents('1.jpg')); //原图url var_dump($qiniu->url('test/1.jpg')); //小图url 规则: "m" var_dump($qiniu->url('test/1.jpg', 'm')); } /** * 阿里云 * 购买9元40GB/年 */ public function ali() { $config = [ 'accessKey' => 'xxxxxxx', 'secret' => 'xxxxxxxxxxxxxxxx', 'endPoint' => '地域节点', 'bucket' => 'OSS名称', ]; $oss = new AliOss($config); // dd(file_exists('1.jpg')); $bool = $oss->put('test.jpg', file_get_contents('1.jpg')); //原图url echo $oss->url('test.jpg'); echo '<br>'; //小图url 规则名称: "s" echo $oss->url('test.jpg', 's'); } }
七牛云的accessKey
阿里的accessKey
阿里的OSS需要将读写的权限设置为公共读
七牛云因为创建的时候选择的是公共空间,所以不需设置