ThinkPHP5+UEditor Image Upload to Alibaba Cloud Object Storage OSS Function Example

  • 2021-12-13 07:44:20
  • OfStack

This article illustrates how ThinkPHP5+UEditor images are uploaded to Alibaba Cloud Object Store OSS. Share it for your reference, as follows:

ThinkPHP5 uses rich text UEditor to modify the local pictures uploaded in the rich text edit box to Alibaba Cloud Object Storage OSS

ThinkPHP5 Loads UEditor

UEditor Download: https://ueditor.baidu.com/website/download. html # ueditor

(Or download from this site: https://www.ofstack.com/codes/56667. html)

Alibaba Cloud Object Store SDK Download: https://github.com/aliyun/aliyun-oss-php-sdk

1. Configuration Items

ueditor directory:\ public\ static\ admin\ lib\ ueditor\ 1.4. 3
OSS configuration file directory:\ application\ config\ oos.php
OSS SDK Directory:\ extend\ oos

2. Code

1. OSS configuration file


<?php
return [
  'endpoint' => 'xxxx',
  'accessKeyId' => 'xxxxxxxxxxx',
  'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
  'bucket' => 'xxxxx',
  'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // For personal configuration, upload pictures to access the complete link of the head 
];

2. Write Oos. class. php controller under UEditor

* Note: I use the relative path, please replace the file import address according to your own directory structure


<?php
require_once realpath(dirname(__FILE__) . '/../../../../../../../') . '/extend/oos/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
class Oos
{
  protected $oos = null;
  protected $bucket = null;
  // Get OOS Client 
  protected function getOssClient(){
    if($this->oos === null){
      $config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
      $this->bucket = $config['bucket'];
      try {
        $this->oos = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint'], false);
      } catch (OssException $e) {
        printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
        printf($e->getMessage() . "\n");
        return null;
      }
    }
    return $this->oos;
  }
  // Upload 
  public function upload($file,$save){
    $config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
    $save = 'upload/'.$save;
    $ossClient = $this->getOssClient();
    if (is_null($ossClient)) exit(' Link storage failed ');
    $result = $ossClient->uploadFile($this->bucket, $save, $file);
    return !empty($result['x-oss-request-id']);
  }
}

3. Modify the PHP file for uploading pictures from UEditor,\ public\ static\ admin\ lib\ ueditor\ 1.4. 3\ php\ action_crawler. php


<?php
/**
 *  Grab remote pictures 
 * User: Jinqn
 * Date: 14-04-14
 * Time:  Afternoon 19:18
 */
set_time_limit(0);
include("Uploader.class.php");
include("Oos.class.php");
//  Introduce oss Object 
$oos_config = require realpath(dirname(__FILE__) . '/../../../../../../../') .'/application/config/oos.php';
$oos = new Oos();
/*  Upload configuration  */
$config = array(
  "pathFormat" => $CONFIG['catcherPathFormat'],
  "maxSize" => $CONFIG['catcherMaxSize'],
  "allowFiles" => $CONFIG['catcherAllowFiles'],
  "oriName" => "remote.png"
);
$fieldName = $CONFIG['catcherFieldName'];
/*  Grab remote pictures  */
$list = array();
if (isset($_POST[$fieldName])) {
  $source = $_POST[$fieldName];
} else {
  $source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
  $item = new Uploader($imgUrl, $config, "remote");
  $info = $item->getFileInfo();
  $year = date('Ymd',time());// Picture path  ( Year / Month )  Set up by yourself 
  $img_name = time().rand(1,1000).$info['type'];
  $bos_url = "ueditor_upload/xinjieshi/image/$year/$img_name";// Use as the path and name of the saved picture 
  $oos->upload($_SERVER['DOCUMENT_ROOT'].'/'.$info['url'],$bos_url);
  array_push($list, array(
    "state" => $info["state"],
    "url" => $oos_config['uploadurl'].$bos_url,
    "size" => $info["size"],
    "title" => htmlspecialchars($info["title"]),
    "original" => htmlspecialchars($info["original"]),
    "source" => htmlspecialchars($imgUrl)
  ));
}
/*  Returning crawled data  */
return json_encode(array(
  'state'=> count($list) ? 'SUCCESS':'ERROR',
  'list'=> $list
));

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on ThinkPHP framework of PHP programming help.


Related articles: