Home > Backend Development > PHP Tutorial > PHP怎么实现上传文件到阿里云OSS

PHP怎么实现上传文件到阿里云OSS

PHPz
Release: 2020-09-04 15:35:47
Original
5411 people have browsed it

PHP怎么实现上传文件到阿里云OSS

PHP实现上传文件到阿里云OSS的方法:

1. 阿里云OSS创建存储空间Bucket(读写权限为:公共读)

2. 拿到相关配置

accessKeyId:*********
accessKeySecret:*********
endpoint:********
bucket:********
Copy after login

3.创建 oss.php 上传类 (基于thinkPHP5)

<?php
namespace app\controller;
use OSS\OssClient;
class Oss {
    private static $_instance;
    private function __construct() {
    }
    private function __clone() {
    }
    /**
     * 获取一个OssClient实例
     * @return null|OssClient
     */
    public static function getInstance() {
        if (!(self::$_instance instanceof OssClient)) {
            try {
                self::$_instance = new OssClient(env(&#39;oss.access_key_id&#39;), env(&#39;oss.access_key_secret&#39;), env(&#39;oss.endpoint&#39;), false);
            } catch (OssException $e) {
                printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
                printf($e->getMessage() . "\n");
                return null;
            }
        }
        return self::$_instance;
    }
    /**
     * 获取bucket
     * @return string
     */
    public static function getBucketName()
    {
        return env(&#39;oss.bucket&#39;);
    }
}
Copy after login

3.上传调用

   use app\controller\Oss;
  .
  .
  .
  
  public function addShopImg(){
        $this->checkParams(&#39;shop_id&#39;);
        $file = $this->request->file(&#39;image&#39;);
        if ($file && ($file->getError() == &#39;&#39;) && $file->checkImg() && $file->checkSize(5*1024*1024)) {
            $info = $file->move(APP_PATH . &#39;../public/upload/shops/&#39;);
            //上传图片至阿里云oss
            $fileName = &#39;biz_oss/upload/shops/&#39; . $info->getFilename();
            $ossClient = Oss::getInstance();
            $bucket = Oss::getBucketName();
            $ossClient->uploadFile($bucket, $fileName, $info->getPathname());
            $data[&#39;shop_img&#39;] = &#39;/upload/shops/&#39;.$info->getFilename();
            $data[&#39;shop_id&#39;] = $this->params[&#39;shop_id&#39;];
            $re = db(&#39;shopImg&#39;)->insert($data);
            if($re){
                Api::output();
            }else{
                Api::fail(2, &#39;上传失败&#39;);
            }
        } else {
            Api::fail(1, &#39;图片不合规&#39;);
        }
    }
Copy after login

4.访问 oss域名地址 不可在浏览器直接访问 可用nginx 代理

配置中加入:

location ^~ /biz_oss {
  proxy_pass http://xxxxxx.oss-cn-shenzhen-internal.aliyuncs.com;
}
Copy after login

重启nginx

nginx配置的域名(server_name)后接上 /biz_oss 如:kwdst.3ce.com/biz_oss 即可指向oss上资源存储的空间

如下 $oss_url = kwdst.3ce.com/biz_oss

<div style="text-align:center; width:100%; height:100%;">
    <img src="{$oss_url}{$img.shop_img}" style="vertical-align:middle;"  />
</div>
Copy after login

 如此浏览器中html 即可访问加载 oss上图片资源。

更多相关技术文章,请访问PHP中文网

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template