Tp5 实现管理Ueditor 百度编辑器上传的图片

2019-06-28   阅读:2672   分类:后端    标签: TP5

知识点:

1、PHP scandir() 函数 函数返回指定目录中的文件和目录的数组。

2、PHP is_dir() 函数 is_dir() 函数检查指定的文件是否是目录。

3、递归

4、PHP str_replace() 函数 函数以其他字符替换字符串中的一些字符

5、PHP unlink() 函数 unlink() 函数删除文件。

6、PHP file_exists() 函数 file_exists() 函数检查文件或目录是否存在。

common.php 文件夹添加,图片处理函数

// 图片资源处理函数
function my_scandir($dir=UEDITOR){
	$flies=array();
	$dir_list=scandir($dir);
	foreach ($dir_list as $file) {
		if($file != '.' && $file !=='..'){
			if(is_dir($dir.'/'.$file)){//判断是否是文件夹
				$flies[$file]=my_scandir($dir.'/'.$file); //递归
			}else{
				$flies[]=$dir.'/'.$file;
			}
		}
	}
	return $flies;
}

index.php 入口文件下定义:

// 定义ueditor目录
define('UEDITOR', __DIR__ . '/../../ueditor');
define('HTTP_UEDITOR','/ueditor');
define('DEL_UEDITOR', __DIR__ . '/../.././');


Ueditor 图片管理

	// Ueditor图片管理
    public function imglist()
    {
    	$_files=my_scandir();
    	$files=array();
    	foreach ($_files as $k => $v) {
    		if(is_array($v)){
    			foreach ($v as $k1 => $v1) {
    				$v1=str_replace(UEDITOR,HTTP_UEDITOR,$v1);
    				$files[]=$v1;
    			}
    		}else{
    			$v=str_replace(UEDITOR,HTTP_UEDITOR,$v);
    			$files[]=$v;
    		}
    	}
    	$this->assign('img_Res',$files);
        return view();
    }
    // 删除图片
    public function delimg(){
    	$imgSrc=input('imgsrc');
    	$imgSrc = DEL_UEDITOR.$imgSrc;
    	if(file_exists($imgSrc)){//判断文件是否存在
             if(@unlink($imgSrc)){
	    		return json(['code'=>200,'message'=>'删除成功']);
	    	}else{
	    		return json(['code'=>500,'message'=>'删除失败']);
	    	}
          }
    }

图片遍历到列表:

image.png

 <table class="table table-striped table-hover table-bordered" id="editabledatatable">
	<thead>
		<tr role="row">
			<th>
				图片
			</th>
			<th class="text-center" width="14%;">
				操作
			</th>
		</tr>
	</thead>

	<tbody>
	{volist name="img_Res" id="img"}
		<tr>
			<td>
					<img src="{$img}" alt="" width="70px"; height="70px">
			</td>
			<td align="center">
				<a href="javascript:;" onclick="delimg(this,'{$img}')" class="btn btn-danger btn-xs delete"><i class="fa fa-trash-o"></i> 删除</a>
			</td>
		</tr>
	{/volist}
	</tbody>
</table>

ajax 删除:

<script>
	function delimg(obj,objid){
		if(!confirm('确定要删除么?')){
			return false;
		}
		$.ajax({
			url:"{:url('admin/Ueditor/delimg')}",
			type:'POST',
			data:{'imgsrc':objid},
			datatype:'json', 
			success:function(data){
				if(data.code==200){}
				alert(data.message);
				// history.go(0);
				$(obj).parent().parent().remove();
			}
		})  
	}
</script>


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/157.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×