PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ThinkPhp教程 > 正文

    thinkphp5整合阿里云oss文件上传

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用thinkphp5实现oss文件上传使用thinkphp5实现oss文件上传,实现静态资源与网站分离。本功能使用到了阿里云oss的php接口,请先下载安装al...
    使用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 = ''

    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.  

    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. //B5教程网www.bcty365.com 
    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.     } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-83-5362-1.html
    相关热词搜索: