加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

发布文章时Kindeditor自动保存远程图片(php)

发布时间:2023-04-27 10:43:31 所属栏目:PHP教程 来源:
导读:前端JS代码:

function checkForm() {//如果勾选了需要保存远程图片则进入保存远程图片函数

if ($("#cBoxSaveRomote")[0].checked) {

saveRomoteImgs();

KE.util.setData("content1");

return true;
前端JS代码:

function checkForm() {//如果勾选了需要保存远程图片则进入保存远程图片函数

if ($("#cBoxSaveRomote")[0].checked) {

saveRomoteImgs();

KE.util.setData("content1");

return true;

function saveRomoteImgs() {

var imgcount = 0;

var content = KE.html("content1");//得到HTML代码

content = content.replace(/src=['"][^'"]+(.bmp|.jpg|.jpeg|.gif|.png)['"]/ig,function(matchword){

imgcount++;

showInfoWindow("图片"+imgcount+"保存开始");//显示提示框

matchArray = /src=['"]([^'"]+)['"]/i.exec(matchword);

var newimgpath = 'src="'+matchArray[1]+'"';

$.ajax({

async:false,//这个很重要,要同步ajax,设为true的话是不等返回的

url:"upload/save_remote_image.PHP",

type:"POST",

data:{imgurl:matchArray[1],referbase:$("#txtReferbase").val()},

dataType:"json",

timeout:10000,

cache:false,

success:function(result){

if (result.error) {

showInfoWindow("图片"+imgcount+"保存失败1("+result.message+")");

//newimgpath = 'src="'+matchArray[1]+'"';

} else {

showInfoWindow("图片"+imgcount+"保存成功");

newimgpath = 'src="'+result.url+'"';

error:function(request,status,error){

showInfoWindow("图片"+imgcount+"保存失败2("+status+";"+error+")");

//newimgpath = 'src="'+matchArray[1]+'"';

return newimgpath;

});

KE.html("content1",content);//把修改好的html内容再保存

clearInfoWindow();//关闭提示框

upload/save_remote_image.PHP代码:

<?PHP

require_once 'JSON.PHP';

session_name("lscj_session");

session_start();

if (!isset($_SESSION['username']))

 session_destroy();

 alert("用户信息丢失,请重新登录!");

if (empty($_POST['imgurl'])) {

 alert("imgurl为空!");

$PHPbb_root_path = "../../../";

//图片显示路径

$imgShowPath = "".date('Y').'/'.date('m').'/';

//图片保存路径

$imgStorePath = $PHPbb_root_path.'attachments/'.date('Y').'/';

if (!is_dir($imgStorePath)) {

 mkdir($imgStorePath,0777);

$imgStorePath .= date('m').'/';

if (!is_dir($imgStorePath)) {

 mkdir($imgStorePath,0777);

$imgurl = $_POST['imgurl'];

$referbase = $_POST['referbase'];

if (0 === stripos($imgurl,"http") || 0 === stripos($imgurl,"//")) {

} else {

 if (empty($referbase)) {

 alert("referbase为空!");

 } else {

 $imgurl = $referbase.$imgurl;

if (false !== stripos($imgurl,"//")) {

 header('Content-type: text/html; charset=utf-8');

 $json = new Services_JSON();

 echo $json->encode(array('error' => 0,'url' => $imgurl));

$filetype = getFiletype($imgurl);

if (empty($filetype)) {

 alert("图片类型为空!");

$newimgname = time().'_'.rand(1000,9999).".".$filetype;

$newimgpath = $imgStorePath.$newimgname;

set_time_limit(0);

$get_file = @file_get_contents($imgurl);

if ($get_file) {

 $fp = @fopen ( $newimgpath,'w');

 @fwrite ( $fp,$get_file);

 @fclose ( $fp);

header('Content-type: text/html; charset=utf-8');

 $json = new Services_JSON();

 echo $json->encode(array('error' => 0,'url' => $imgShowPath.$newimgname));

exit;

} else {

 alert("获取图片失败!");

function getFiletype($filename) {

 $tempArray = explode(".",$filename);//分割字符串

 if (count($tempArray)>1) {

 $fileType = $tempArray[count($tempArray)-1];//得到文件扩展名

 return $fileType;

 return "";

function alert($msg) {

 header('Content-type: text/html; charset=utf-8');

 $json = new Services_JSON();

 echo $json->encode(array('error' => 1,'message' => $msg));

 exit;

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章