그누보드 스킨

그누보드 에디터 글 이미지 수정시 기존 이미지파일이 남는것 삭제하는 로직

헤롱헤롱이 2021. 2. 16. 18:00

에디터이미지더미데이터삭제.zip
0.00MB

 

 

<?php
include_once('./common.php');
ini_set('memory_limit','-1');

$res = sql_query(" SHOW TABLES ");
$user = sql_fetch("SELECT * FROM information_schema.SCHEMA_PRIVILEGES WHERE PRIVILEGE_TYPE = 'DELETE'");
//모든게시판을 읽어드린다

$tmpTables = array();
$imgs = array();
$DbName = "Tables_in_{$user["TABLE_SCHEMA"]}";
while($row = sql_fetch_array($res))
{
	//게시판테이블인경우만 내용필드를 읽어 이미지경로만 추출하여 배열에 담는다
    if(preg_match("/g5_write_/", $row[$DbName]))
    {
        $bo_table = str_replace("g5_write_","", $row[$DbName]);
        $selTmpSql = " SELECT *, '$bo_table' as num  FROM  g5_write_{$bo_table} ";
        
        $selTmps = sql_query($selTmpSql);
        
        while($selTmp = sql_fetch_array($selTmps))
        {
            $matches = get_editor_image($selTmp['wr_content'], false);
            
            foreach($matches[1] as $key => $val)
            {
                $tmpFile = explode("/data/editor/", $val);
                $tmps = explode("/", $tmpFile[1]);
                $imgs[$tmps[0]][] = $tmps[1];
            }
            
        }
    }

	//영카트 상점이용하는경우 상품내용에 이미지가 있는경우 이미지경로만 배열에 담는다
    if(preg_match("/g5_shop_item/", $row[$DbName]))
    {
        
        $selTmpSql = " SELECT * FROM  g5_shop_item ";
        
        $selTmps = sql_query($selTmpSql);
        
        while($selTmp = sql_fetch_array($selTmps))
        {
            $matches = get_editor_image($selTmp['it_explan'], false);
            
            foreach($matches[1] as $key => $val)
            {
                $tmpFile = explode("/data/editor/", $val);
                $tmps = explode("/", $tmpFile[1]);
                $imgs[$tmps[0]][] = $tmps[1];
            }

            $matches2 = get_editor_image($selTmp['it_mobile_explan'], false);
            
            foreach($matches2[1] as $key => $val)
            {
                $tmpFile = explode("/data/editor/", $val);
                $tmps = explode("/", $tmpFile[1]);
                $imgs[$tmps[0]][] = $tmps[1];
            }
            
        }
    }


    
}

//파일저장경로의 모든파일을 읽어 배열에 저장한다
$dir = G5_DATA_PATH."/editor";

$dbImgs = array();

$result_array = array();
$handle = opendir($dir);
$cnt = 0;
while($file = readdir($handle))
{
    
    if($file == '.'||$file == '..') continue;
    
    if (is_dir($dir."/".$file))
    {
        $ffs = scandir($dir."/".$file);
        
        foreach($ffs as $key => $val)
        {
            if($val == "." || $val == "..") continue;
            
            //if(preg_match("/thumb-/", $val)) continue;
            
            $dbImgs[$file][] = $val;
        }
    }
    
}

closedir($handle);

foreach($dbImgs as $key => $val)
{

    foreach($val as $subKey => $subVal)
    {
        if(!in_array($subVal, $imgs[$key]))
        {
            @unlink($dir."/".$key."/".$subVal);
        }
        else
        {
            echo $dir."/".$key."/".$subVal."\n";
        }
    }
}

//현재 저장폴더파일을 모든 에디터 이미지배열에 있는지 체크하여 없는경우 파일을 삭제한다


?>