think php上传服务器(ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能示例)
类别:编程学习 浏览量:593
时间:2022-01-14 02:39:57 think php上传服务器
ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能示例本文实例讲述了ThinkPHP5+UEditor图片上传到阿里云对象存储OSS。分享给大家供大家参考,具体如下:
ThinkPHP5使用富文本UEditor,将富文本编辑框内上传在本地的图片,修改到阿里云对象存储OSS
ThinkPHP5加载UEditor ···· 略
UEditor下载:https://ueditor.baidu.com/website/download.html#ueditor
阿里云对象存储SDK下载:https://github.com/aliyun/aliyun-oss-php-sdk
一、配置项
ueditor目录:\public\static\admin\lib\ueditor\1.4.3
OSS配置文件目录:\application\config\oos.php
OSS SDK目录:\extend\oos
二、代码
1、OSS配置文件
|
<?php return [ 'endpoint' => 'xxxx' , 'accessKeyId' => 'xxxxxxxxxxx' , 'accessKeySecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx' , 'bucket' => 'xxxxx' , 'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' , //个人配置用,上传图片访问头部完整链接 ]; |
2、在UEditor下写入Oos.class.php控制器
*注:本人用的是相对路径,请自行对照自己的目录结构替换掉文件引入地址
|
<?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; //获取OOS客户端 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; } //上传 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 ( '链接存储失败' ); $result = $ossClient ->uploadFile( $this ->bucket, $save , $file ); return ! empty ( $result [ 'x-oss-request-id' ]); } } |
3、修改UEditor 上传图片的PHP文件,\public\static\admin\lib\ueditor\1.4.3\php\action_crawler.php
|
<?php /** * 抓取远程图片 * User: Jinqn * Date: 14-04-14 * Time: 下午19:18 */ set_time_limit(0); include ( "Uploader.class.php" ); include ( "Oos.class.php" ); // 引入oss对象 $oos_config = require realpath (dirname( __FILE__ ) . '/../../../../../../../' ) . '/application/config/oos.php' ; $oos = new Oos(); /* 上传配置 */ $config = array ( "pathFormat" => $CONFIG [ 'catcherPathFormat' ], "maxSize" => $CONFIG [ 'catcherMaxSize' ], "allowFiles" => $CONFIG [ 'catcherAllowFiles' ], "oriName" => "remote.png" ); $fieldName = $CONFIG [ 'catcherFieldName' ]; /* 抓取远程图片 */ $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()); //图片路径 (年/月) 自己设置 $img_name = time().rand(1,1000). $info [ 'type' ]; $bos_url = "ueditor_upload/xinjieshi/image/$year/$img_name" ; //用作保存的图片路径和名字 $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 ) )); } /* 返回抓取数据 */ return json_encode( array ( 'state' => count ( $list ) ? 'SUCCESS' : 'ERROR' , 'list' => $list )); |
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_28285379/article/details/87972847
您可能感兴趣
- thinkphp5单例原理(Thinkphp5框架简单实现钩子Hook行为的方法示例)
- thinkphp5开发小程序后台(PHP小程序支付功能完整版基于thinkPHP)
- thinkphp从模板自定义标签(Thinkphp5.0框架视图view的循环标签用法示例)
- vuephp后台开发框架(Vue+thinkphp5.1+axios实现文件上传)
- thinkphp5数据库配置(Thinkphp5框架实现获取数据库数据到视图的方法)
- thinkphp5.1请求流程(ThinkPHP5.0框架使用build 自动生成模块操作示例)
- thinkphp5.1手动连接mysql数据库(thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例)
- thinkphp5框架怎么打开(thinkphp5.1框架中容器Container和门面Facade的实现方法分析)
- thinkphp继承model如何使用(Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解)
- thinkphp5怎么设置默认返回(thinkphp5.1框架实现格式化mysql时间戳为日期的方式小结)
- thinkphp框架案例(thinkphp5.1框架容器与依赖注入实例分析)
- thinkphp5表单系统(thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例)
- thinkphp5框架怎么打开(thinkphp5.1框架模板布局与模板继承用法分析)
- thinkphp快速入门手册(从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解)
- thinkphp5开发教程(thinkPHP5框架接口写法简单示例)
- thinkphp5.1修改(ThinkPHP 5.1 跨域配置方法)
- 今天要吃什么(今天要吃什么菜好)
- 网红直播可以赚很多钱吗(网红直播可以赚很多钱吗)
- 今天是什么日子(今天是什么日子有什么特殊意义吗)
- 这里输入关键词(怎么输入关键词搜索)
- 34岁的舒畅,就这样走到了末路,不知会不会后悔15年前的草率决定(就这样走到了末路)
- 不走心的古装造型 舒畅 毁容式 出演,萧蔷雷出新高度(不走心的古装造型)
热门推荐
- python2.7连接sqlserver数据库(Python使用sqlalchemy模块连接数据库操作示例)
- sqlserver分页数据重复(SQL Server 在分页获取数据的同时获取到总记录数)
- js三级联动列表(基于JavaScript实现年月日三级联动)
- html5怎么滚动屏幕(HTML5 video进入全屏和退出全屏的实现方法)
- docker的postgres镜像包(Docker中运行PostgreSQL并推荐几款连接工具)
- 自己怎么选网站服务器(网站空间服务器的正确选择方法)
- filezilla如何连接远程服务器(使用FileZilla快速搭建FTP文件服务的图文教程)
- Visual Studio 自动添加头部注释
- 宝塔linux面板搭专业版(宝塔linux面板怎么卸载?)
- python转pdf教程(Python实现将HTML转成PDF的方法分析)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9