Ueditor图片上传转存

为了减小垃圾数据(操做失误的图片),在ueditor上传图片时,先存储在指定临时文件夹,当数据提交到服务器时再将ueditor内容中的图片逐一保存到指定的图片文件夹php

1.找到./php/config.json,修改成本身指定的路径,正则表达式

/*"imagePathFormat": "/Public/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,能够自定义保存路径和文件名格式 */
"imagePathFormat": "/temp/upload/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,能够自定义保存路径和文件名格式 */

2.当ueditor内容提交到对应方法时json

$str = $this->input->post('editorValue');
if ( strstr($str, '<img')){
    $match  = imgSelect($str);
    $newstr = str_replace('/temp/upload/'.date('Ymd'),$data['config']['image_url'].'bbs/'.date('Ym'),$str);
}else{
    $newstr = $str;
}

imgSelect (),这个是自定义匹配图片路径的正则表达式数组

$newmatch  =  imgSelect ($newstr);
foreach ($newmatch[1] as $newmatkey => $newmatval){
    foreach ($match[1] as $matkey => $matval){
        if(substr($newmatval,strpos($newmatval,date('Ym').'/')+7) == substr($matval,strpos($matval,date('Ymd').'/')+9)) {
            $newmatval = substr($newmatval, strpos($newmatval, '.com/') + 4);
            $data_file = file_get_contents(str_replace('//t', '/t', $data['config']['base_url'] . $matval));
            file_put_contents('..' . $newmatval, $data_file);
        }

    }
}

自定义 imgSelect ()服务器

/***************
 * @function       img标签过滤
 * @Param:         $str :传入参数
 *@Return:         $match :数组
 * 提示:
 ***************/
function  imgSelect ($str){
    $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
    preg_match_all($pattern,$str,$match);
    return $match;
}
相关文章
相关标签/搜索