原理描述:
在原有织梦后台菜单中增加"清理冗余图片按钮",实现清理冗余图片的功能.
操作步骤:
1. 打开后台dede\sys_sql_query.php代码
在该文件中搜索如下代码:
//修复全部表
else if($dopost=="repairAll")
{
$dsql->SetQuery("Show Tables");
$dsql->Execute('t');
while($row = $dsql->GetArray('t',MYSQL_BOTH))
{
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}` ");
if($rs)
{
echo "修复表: {$row[0]} ok!<br />\r\n";
} else {
echo "修复表: {$row[0]} 失败! 原因是: ".$dsql->GetError()."<br />\r\n";
}
}
exit();
}
else if($dopost=="repairAll")
{
$dsql->SetQuery("Show Tables");
$dsql->Execute('t');
while($row = $dsql->GetArray('t',MYSQL_BOTH))
{
$rs = $dsql->ExecuteNoneQuery("REPAIR TABLE `{$row[0]}` ");
if($rs)
{
echo "修复表: {$row[0]} ok!<br />\r\n";
} else {
echo "修复表: {$row[0]} 失败! 原因是: ".$dsql->GetError()."<br />\r\n";
}
}
exit();
}
在该分支后补充如下代码:
//清理冗余图片
else if($dopost=="delerrpic")
{
ini_set('max_execution_time',1800);//单位秒
ini_set('max_input_time',1800);//单位秒
//echo ini_get('max_execution_time')."\n";
$file_a=array();
$err_img_cnt = 0;
function rFile($p){
global $file_a;
global $dsql;
global $err_img_cnt;
$handle=opendir($p);
$dir_a=array();
while ($file = readdir($handle)) {
if($file!="." && $file!=".."){
$tmp=$p."/".$file;
echo "<br />dir: ".$tmp;
if(is_dir($tmp)){
$dir_a[count($dir_a)]=$tmp;
}elseif(is_file($tmp)){
$file_a[count($file_a)]=$tmp;
$v = $tmp;
$temp=substr($v,2);
$query = "select count(*) as cnt from dede_addonimages where imgurls like '%".$temp."%'";
//echo "query sql: ".$query."<br />";
$dsql->setquery($query);
$dsql->execute();
while($row = $dsql->getarray())
{
//print_r($row);
if($row['cnt']==0){
if(strpos($v, 'del_') == FALSE && substr($v, -7, 3)!="-lp" && substr($v, -8, 4)!="_lit" && substr($v, -10, 5)!="index"){
if(file_exists($v)) {
unlink($v);
echo "<br />unlink: ".$v;
$err_img_cnt++;
}
}
}
}
}
}
}
closedir($handle);
foreach($dir_a as $v){
rFile($v);
}
}
rFile("../uploads/allimg/171003");//调用,要遍历的目录
$dsql->Close();
ShowMsg("成功删除冗余图片, 共: ".$err_img_cnt."张!","javascript:;");
exit();
}
else if($dopost=="delerrpic")
{
ini_set('max_execution_time',1800);//单位秒
ini_set('max_input_time',1800);//单位秒
//echo ini_get('max_execution_time')."\n";
$file_a=array();
$err_img_cnt = 0;
function rFile($p){
global $file_a;
global $dsql;
global $err_img_cnt;
$handle=opendir($p);
$dir_a=array();
while ($file = readdir($handle)) {
if($file!="." && $file!=".."){
$tmp=$p."/".$file;
echo "<br />dir: ".$tmp;
if(is_dir($tmp)){
$dir_a[count($dir_a)]=$tmp;
}elseif(is_file($tmp)){
$file_a[count($file_a)]=$tmp;
$v = $tmp;
$temp=substr($v,2);
$query = "select count(*) as cnt from dede_addonimages where imgurls like '%".$temp."%'";
//echo "query sql: ".$query."<br />";
$dsql->setquery($query);
$dsql->execute();
while($row = $dsql->getarray())
{
//print_r($row);
if($row['cnt']==0){
if(strpos($v, 'del_') == FALSE && substr($v, -7, 3)!="-lp" && substr($v, -8, 4)!="_lit" && substr($v, -10, 5)!="index"){
if(file_exists($v)) {
unlink($v);
echo "<br />unlink: ".$v;
$err_img_cnt++;
}
}
}
}
}
}
}
closedir($handle);
foreach($dir_a as $v){
rFile($v);
}
}
rFile("../uploads/allimg/171003");//调用,要遍历的目录
$dsql->Close();
ShowMsg("成功删除冗余图片, 共: ".$err_img_cnt."张!","javascript:;");
exit();
}
2. 修改模板htm文件, 打开dede\templets\sys_sql_query.htm
<div style="float:left">
<input type="Submit" name="Submit5" value="优化全部表" class="coolbg np" onClick="this.form.dopost.value='opimizeAll';" />
<br />
<input type="Submit" name="Submit6" value="修复全部表" class="coolbg np" onClick="this.form.dopost.value='repairAll';" style="margin-top:6px;" />
<br />
<input type="Submit" name="Submit7" value="清理冗余图片" class="coolbg np" onClick="this.form.dopost.value='delerrpic';" style="margin-top:6px;" />
</div>
<input type="Submit" name="Submit5" value="优化全部表" class="coolbg np" onClick="this.form.dopost.value='opimizeAll';" />
<br />
<input type="Submit" name="Submit6" value="修复全部表" class="coolbg np" onClick="this.form.dopost.value='repairAll';" style="margin-top:6px;" />
<br />
<input type="Submit" name="Submit7" value="清理冗余图片" class="coolbg np" onClick="this.form.dopost.value='delerrpic';" style="margin-top:6px;" />
</div>