有時(shí)候我們刪除文章的時(shí)候,文章的圖片還是會(huì)留存在你的圖片文件夾上面,這樣很占服務(wù)器的內(nèi)存,程序也會(huì)變得很大,所以我們需要?jiǎng)h除文章的同時(shí)把文章里的圖片也對(duì)應(yīng)的刪掉,
但織夢(mèng)本身是不支持的,需要經(jīng)過小的二次開發(fā)才可以,下面織夢(mèng)無憂符老師教你們?cè)趺醋?,?jīng)過測(cè)試可以用的,
/include/extend.func.php
復(fù)制以下的全部代碼直接覆蓋 到 extend.func.php 文件
<?php function GetPicsTruePath($body,$litpic) //解析body數(shù)據(jù),獲得所有圖片的絕對(duì)地址 { $delfiles = array();//存儲(chǔ)圖片地址數(shù)據(jù) if(!empty($litpic)) { $litpicpath = GetTruePath(); $litpicpath .= $litpic; $delfiles[] = $litpicpath;//縮略圖地址 } preg_match_all("/src=[\"|'|\S|\s]([^ |\/|>]*){0,}(([^>]*)\.(gif|jpg|png))/isU",$body,$tmpdata); $picspath = array_unique($tmpdata[2]);//body中所有圖片的地址 foreach($picspath as $tmppath) { $path = GetTruePath();//獲得絕對(duì)路徑 $picpath = preg_replace("/[a-zA-z]+:\/\/[^ |\/|\s]*/",'',$tmppath);//去掉網(wǎng)址部分 $path .=$picpath; $delfiles[] = $path;//保存處理后的數(shù)據(jù) } return $delfiles; } function WriteToDelFiles($msg)//刪除文章的時(shí)候會(huì)通過此函數(shù)記錄日志 { if(empty($msg)) $savemsg="未獲得消息"; else $savemsg = $msg; $errorFile = dirname(__FILE__).'/../data/del_body_file.txt';//刪除記錄文件 $fp = @fopen($errorFile, 'a'); @fwrite($fp,"\r\n{$savemsg}"); @fclose($fp); } //獲得文章Body數(shù)據(jù) function GetArcBody($aid) { global $dsql; $query = "SELECT dede_addonarticle.body FROM dede_addonarticle WHERE dede_addonarticle.aid = '$aid'"; $row = $dsql->GetOne($query); if(is_array($row)) return $row; else return false; } function litimgurls($imgid=0){ global $lit_imglist; $dsql = new DedeSql(false); //獲取附加表 $row = $dsql->GetOne("SELECT c.addtable FROM dede_archives AS a LEFT JOIN dede_channeltype AS c ON a.channel=c.id where a.id='$imgid'"); $addtable = trim($row['addtable']); //獲取圖片附加表imgurls字段內(nèi)容進(jìn)行處理 $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'"); //調(diào)用inc_channel_unit.php中ChannelUnit類 $ChannelUnit = new ChannelUnit(2,$imgid); //調(diào)用ChannelUnit類中GetlitImgLinks方法處理縮略圖 $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']); //返回結(jié)果 return $lit_imglist; } ?> |
<?php function DelArc($aid,$type='ON',$onlyfile=false) { global $dsql,$cfg_cookie_encode,$cfg_multi_site,$cfg_medias_dir; global $cuserLogin,$cfg_upload_switch,$cfg_delete,$cfg_basedir; global $admin_catalogs, $cfg_admin_channel; if($cfg_delete == 'N') $type = 'OK'; if(empty($aid)) return ; $aid = ereg_replace("[^0-9]", '', $aid); $arctitle = $arcurl = ''; //查詢表信息 $query = "Select ch.maintable,ch.addtable,ch.nid,ch.issystem From `dede_arctiny` arc left join `dede_arctype` tp on tp.id=arc.typeid left join `dede_channeltype` ch on ch.id=arc.channel where arc.id='$aid' "; $row = $dsql->GetOne($query); $nid = $row['nid']; $maintable = (trim($row['maintable'])=='' ? 'dede_archives' : trim($row['maintable'])); $addtable = trim($row['addtable']); $issystem = $row['issystem']; //查詢檔案信息 if($issystem==-1) { $arcQuery = "Select arc.*,tp.* from `$addtable` arc left join `dede_arctype` tp on arc.typeid=tp.id where arc.aid='$aid' "; } else { $arcQuery = "Select arc.*,tp.*,arc.id as aid from `$maintable` arc left join `dede_arctype` tp on arc.typeid=tp.id where arc.id='$aid' "; } $arcRow = $dsql->GetOne($arcQuery); $arcBodyRow = GetArcBody($aid); //檢測(cè)權(quán)限 if(!TestPurview('a_Del,sys_ArcBatch')) { if(TestPurview('a_AccDel')) { if( !in_array($arcRow['typeid'], $admin_catalogs) && (count($admin_catalogs) != 0 || $cfg_admin_channel != 'all') ) { return false; } } else if(TestPurview('a_MyDel')) { if($arcRow['mid'] != $cuserLogin->getUserID()) { return false; } } else { return false; } } //$issystem==-1 是單表模型,不使用回收站 if($issystem == -1) $type = 'OK'; if(!is_array($arcRow)) return false; /** 刪除到回收站 **/ if($cfg_delete == 'Y' && $type == 'ON') { $dsql->ExecuteNoneQuery("Update `$maintable` set arcrank='-2' where id='$aid' "); $dsql->ExecuteNoneQuery("Update `dede_arctiny` set `arcrank` = '-2' where id = '$aid'; "); } else { //刪除數(shù)據(jù)庫記錄 if(!$onlyfile) { //刪除相關(guān)附件 if($cfg_upload_switch == 'Y') { $dsql->Execute("me", "SELECT * FROM `dede_uploads` WHERE arcid = '$aid'"); while($row = $dsql->GetArray('me')) { $addfile = $row['url']; $aid = $row['aid']; $dsql->ExecuteNoneQuery("Delete From `dede_uploads` where aid = '$aid' "); $upfile = $cfg_basedir.$addfile; if(@file_exists($upfile)) @unlink($upfile); } } $dsql->ExecuteNoneQuery("Delete From `dede_arctiny` where id='$aid'"); if($addtable != '') { $dsql->ExecuteNoneQuery("Delete From `$addtable` where aid='$aid' "); } if($issystem != -1) { $dsql->ExecuteNoneQuery("Delete From `dede_archives` where id='$aid' "); } $dsql->ExecuteNoneQuery("Delete From `dede_feedback` where aid='$aid' "); $dsql->ExecuteNoneQuery("Delete From `dede_member_stow` where aid='$aid' "); $dsql->ExecuteNoneQuery("Delete From `dede_taglist` where aid='$aid' "); $dsql->ExecuteNoneQuery("Delete From `dede_erradd` where aid='$aid' "); } //刪除文本數(shù)據(jù) $filenameh = DEDEDATA."/textdata/".(ceil($aid/5000))."/{$aid}-".substr(md5($cfg_cookie_encode),0,16).".txt"; if(@is_file($filenameh)) @unlink($filenameh); } if(empty($arcRow['money'])) $arcRow['money'] = 0; if(empty($arcRow['ismake'])) $arcRow['ismake'] = 1; if(empty($arcRow['arcrank'])) $arcRow['arcrank'] = 0; if(empty($arcRow['filename'])) $arcRow['filename'] = ''; //刪除HTML if($arcRow['ismake']==-1 || $arcRow['arcrank']!=0 || $arcRow['typeid']==0 || $arcRow['money']>0) { return true; } //強(qiáng)制轉(zhuǎn)換非多站點(diǎn)模式,以便統(tǒng)一方式獲得實(shí)際HTML文件 $GLOBALS['cfg_multi_site'] = 'N'; $arcurl = GetFileUrl($arcRow['aid'],$arcRow['typeid'],$arcRow['senddate'],$arcRow['title'],$arcRow['ismake'], $arcRow['arcrank'],$arcRow['namerule'],$arcRow['typedir'],$arcRow['money'],$arcRow['filename']); if(!ereg("\?", $arcurl)) { $htmlfile = GetTruePath().str_replace($GLOBALS['cfg_basehost'],'',$arcurl); if(file_exists($htmlfile) && !is_dir($htmlfile)) { @unlink($htmlfile); $arcurls = explode(".", $htmlfile); $sname = $arcurls[count($arcurls)-1]; $fname = ereg_replace("(\.$sname)$", "", $htmlfile); for($i=2; $i<=100; $i++) { $htmlfile = $fname."_{$i}.".$sname; if( @file_exists($htmlfile) ) @unlink($htmlfile); else break; } } } //解析Body中的資源,并刪除 $willDelFiles = GetPicsTruePath($arcBodyRow['body'],$arcRow['litpic']); $nowtime = time(); $executetime = MyDate('Y-m-d H:i:s',$nowtime);//獲得執(zhí)行時(shí)間 $msg = "\r\n文章標(biāo)題:$arcRow[title]"; WriteToDelFiles($msg); if(!empty($willDelFiles)) { foreach($willDelFiles as $file) { if(file_exists($file) && !is_dir($file)) { if(unlink($file)) $msg = "\r\n位置:$file\r\n結(jié)果:刪除成功!\r\n時(shí)間:$executetime"; else $msg = "\r\n位置:$file\r\n結(jié)果:刪除失??!\r\n時(shí)間:$executetime"; } else $msg = "\r\n位置:$file\r\n結(jié)果:文件不存!\r\n時(shí)間:$executetime"; WriteToDelFiles($msg); }//END foreach } else { $msg = "\r\n未在Body中解析到數(shù)據(jù)\r\nBody原始數(shù)據(jù):$arcBodyRow[body]\r\n時(shí)間:$executetime"; WriteToDelFiles($msg); } return true; } //獲取真實(shí)路徑 function GetTruePath($siterefer='', $sitepath='') { $truepath = $GLOBALS['cfg_basedir']; return $truepath; } ?> |
版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: dedecms 刪除文章同時(shí)刪除圖片的方法