테스트 사이트 - 개발 중인 베타 버전입니다

디렉토리내의 모든 원소의 소유권, 그룹, 퍼미션 정보 가져와서 저장하기 입니다.

· 10년 전 · 209

윈도우 리눅스 공용입니다.
단 윈도우의 경우에는 소유권, 그룹정보는 가져오지 못합니다.

여러가지 활요예가 있겠지만
이런경우에 유용합니다.
압축을 하고 전혀 다른 계정에 압축을 풀었을때
퍼미션이나 소유권 문제때문에 고민할때 사용하면 유용합니다.

이하 소스입니다.

function Ysave_dir_owner_perm($dir, $debug=false){

  if (!is_dir($dir)) return false;

  if (!preg_match("`/$`", $dir)) $dir .= '/';

  if (preg_match("`win`i", PHP_OS)) $os = 'windows';
  else  $os = 'unix or linux';

  $text = '';

  $d = dir($dir);
  while (false !== ($entry = $d->read())) {

    if (substr($entry, 0, 1) == '.') continue;

    if ($os != 'windows') $text .= 'chown ' . fileowner($dir . $entry) . '.' . filegroup($dir . $entry) . ' ' . $dir . $entry . " \n";
     $text .= 'chmod ' . substr(decoct(fileperms($dir . $entry)), -4) . ' ' . $dir . $entry . " \n";
     if ($debug == true) {
     
      if ($os != 'windows') echo 'chown ' . fileowner($dir . $entry) . '.' . filegroup($dir . $entry) . ' ' . $dir . $entry . " <br>\n";
       echo 'chmod ' . substr(decoct(fileperms($dir . $entry)), -4) . ' ' . $dir . $entry . " <br>\n";
     }
   
    if (is_dir($dir . $entry)){

      $text .= Ysave_dir_owner_perm($dir . $entry, $debug);
    }
    else{

      continue;
    }
  }
  $d->close();
 
  return $text;
}

function Ysave_file($file, $text, $perm=0777){

  if (empty($file)) return false;
  $fp = @fopen ($file, 'w');
  if (empty($fp)) return false;

  @fwrite ($fp, $text);
  @fclose($fp);

  return @chmod ($file, $perm);
}

$dir = './';
$text = Ysave_dir_owner_perm($dir, true);
if ($text == false) echo '디렉토리내의 정보를 읽는데 실패하였습니다.';
$save_result = Ysave_file('./temp/owner_perm.txt', $text, 0777);
if (empty($save_result)) echo '파일저장에 실패하였습니다.';  

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
12072
12071
12070
12069
12068
12067
12066
12065
20376
12064
12063
12062
12061
334
12052
12051
328
327
20375
12050
12049
325
12047
319
12045