에디터로 올린 이미지 삭제하는 코드 (썸네일도 삭제) 정보
에디터로 올린 이미지 삭제하는 코드 (썸네일도 삭제)본문
현재 그누보드는 에디터로 첨부한 이미지는 글 삭제시에도 함께 삭제되지 않고 서버에 남아있습니다.
아래 코드를 적용하시면 에디터로 첨부한 이미지가 있는 글을 삭제할 경우 해당 이미지도 삭제하게 됩니다.
/lib/common.lib.php 에서 기존
// 에디터 썸네일 삭제
function delete_editor_thumbnail($contents)
함수 내용을 아래와 같이 변경한다.
// 에디터 썸네일 삭제
function delete_editor_thumbnail($contents)
{
if(!$contents)
return;
// $contents 중 img 태그 추출
$matchs = get_editor_image($contents, false);
if(!$matchs)
return;
for($i=0; $i<count($matchs[1]); $i++) {
// 이미지 path 구함
$imgurl = @parse_url($matchs[1][$i]);
if(strpos($imgurl['path'], "/data/") != 0) {
$data_path = preg_replace("/^\/.*\/data/", "/data", $imgurl['path']);
} else {
$data_path = $imgurl['path'];
}
$is_destfile = false;
if(preg_match('/(gif|jpe?g|bmp|png)$/i', strtolower(end(explode('.', $data_path))))){
$destfile = ( ! preg_match('/\w+\/\.\.\//', $data_path) ) ? G5_PATH.$data_path : '';
if($destfile && preg_match('/\/data\/editor\/[A-Za-z0-9_]{1,20}\//', $destfile) && is_file($destfile)) {
$is_destfile = true;
}
}
if($is_destfile) {
//원본파일 삭제
@chmod($destfile, G5_FILE_PERMISSION);
@unlink($destfile);
//썸네일파일 삭제
$files = glob(dirname($destfile).'/thumb-'.preg_replace("/\.[^\.]+$/i", "", basename($imgurl['path'])).'*');
//return $files;
if (is_array($files)) {
foreach($files as $filename)
unlink($filename);
}
}
}
}
11
댓글 14개
글 작성하면서 에디터를 통해 업로드한 이미지를 다른 게시글에도 사용한 경우
그것을 체크해서 제외하는 로직이 추가되면 좋겠다는 생각을 해봤습니다.
예상컨데 그 상황에서는 지워지지 않을 것 같습니다.
다만 해당 게시물 삭제시 에디터에서 del했던 이미지(게시글에서는 보이지 않지만 서버에 남아있는)도 같이 삭제될 것으로 보입니다.
삭제시 잘 작동이 됩니다.
그러나 글 이동시에도 삭제가 되어버리네요 ㅠㅠ;
글 이동시에는 삭제가 안되었으면 좋겠습니다