关于TP5上传OSS

使用thinkphp5实现oss文件上传
使用thinkphp5实现oss文件上传,实现静态资源与网站分离。本功能使用到了阿里云oss的php接口,请先下载安装aliyun-oss-php-sdk到项目扩展文件夹中,具体请参照oss的接口文档。

1、配置文件oss.php

  1. <?php 
  2. final class oss_config {
  3.     const OSS_ACCESS_ID = '';
  4.     const OSS_ACCESS_KEY = '';
  5.     const OSS_ENDPOINT = '';
  6.     const OSS_BUCKET = '';
  7. }

复制代码

 

2、新建oss类,实现单例

  1. <?php
  2.  
  3. namespace base;
  4.  
  5. use OSS\OssClient;
  6.  
  7. class Oss {
  8.  
  9.     const accessKeyId = \oss_config::OSS_ACCESS_ID;
  10.     const accessKeySecret = \oss_config::OSS_ACCESS_KEY;
  11.     const endpoint = \oss_config::OSS_ENDPOINT;
  12.     const bucket = \oss_config::OSS_BUCKET;
  13.  
  14.     private static $_instance;
  15.  
  16.  
  17.     /**
  18.      * 构造函数
  19.      * Oss constructor.
  20.      */
  21.     private function __construct() {
  22.  
  23.     }
  24.  
  25.     /**
  26.      * 克隆
  27.      */
  28.     private function __clone() {
  29.  
  30.     }
  31.  
  32.     /**
  33.      * 获取一个OssClient实例
  34.      * @return null|OssClient
  35.      */
  36.     public static function getInstance() {
  37.         if (!(self::$_instance instanceof OssClient)) {
  38.             try {
  39.                 self::$_instance = new OssClient(self::accessKeyId, self::accessKeySecret, self::endpoint, false);
  40.             } catch (OssException $e) {
  41.                 printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
  42.                 printf($e->getMessage() . "\n");
  43.                 return null;
  44.             }
  45.         }
  46.         return self::$_instance;
  47.     }
  48.  
  49.     /**
  50.      * 获取bucket
  51.      * @return string
  52.      */
  53.     public static function getBucketName()
  54.     {
  55.         return self::bucket;
  56.     }
  57.  
  58. }

复制代码

 

3、html模板

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6. </head>
  7. <body>
  8. <form enctype="multipart/form-data" method="post" name="fileinfo" action="/index/test/index">
  9.     <table>
  10.         <tr>
  11.             <td>上传文件:</td>
  12.             <td><input type="file" name="image"></td>
  13.         </tr>
  14.         <tr>
  15.             <td colspan="2"><input type="submit" value="上传"></td>
  16.         </tr>
  17.     </table>
  18. </form>
  19.  
  20. </body>
  21. </html>

复制代码

 

4、上传服务器端

  1. <?php
  2.  
  3. namespace osc\index\controller;
  4.  
  5. use osc\common\controller\HomeBase;
  6. use base\Oss;
  7.  
  8.  
  9. class Test extends HomeBase {
  10.     public function index() {
  11.  
  12.         if (request()->isPost()) {
  13.             try {
  14.  
  15.                 $file = request()->file('image');
  16.  
  17.                 if (empty($file)) {
  18.                     die('请选择上传的文件');
  19.                 }
  20.  
  21.                 $allow_max_size = 2 * pow(1024, 2);
  22.                 $allow_upload_ext = ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'wbmp'];
  23.                 $path = ROOT_PATH . 'public' . DS . 'uploads';
  24.  
  25.                 $info = $file->validate(['size' => $allow_max_size, 'ext' => $allow_upload_ext])->move($path);
  26.                 if (!$info) {
  27.                     var_dump($file->getError());
  28.                     die();
  29.                 }
  30.  
  31.                 $fileName = 'uploads/' . $info->getSaveName();
  32.                 $ossClient = Oss::getInstance();
  33.                 $bucket = Oss::getBucketName();
  34.  
  35.                 $ossClient->uploadFile($bucket, $fileName, $info->getPathname());
  36.  
  37.  
  38.             } catch (OssException $e) {
  39.                 return $e->getMessage();
  40.             }
  41.  
  42.         } else {
  43.             return $this->fetch();
  44.         }
  45.     }
  46. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值