PHP CLI 예제] 도스 DIR / ls 명령 구현 소스; 파일 목록(File List) 얻기
Friday, May 18, 2007
스폰서 링크현재 디렉토리에 있는 여러 개의 파일들에 대해 일괄적으로 작업할 때에는, 파일 목록을 구해야 합니다. 다음은 도스의 DIR 또는 유닉스 ls 명령처럼, 현재 디렉토리에 있는 파일들의 이름을 출력하는데, 이 소스를 고쳐서 파일 일괄 작업에 응용할 수 있습니다.
파일명: dir.php
실행 결과 화면:
☞ PHP
현재 디렉토리의 모든 파일 목록 구하기/출력
파일명: dir.php
<?php
$dirname = ".";
$counter_file = 0;
$counter_dir = 0;
if (!$dh = @opendir($dirname)) {
fwrite(STDERR, "에러: 디렉토리를 열 수 없습니다.");
exit(1);
}
while (($file = readdir($dh)) !== false) {
if ($file == "." || $file == "..") continue; // . 과 .. 디렉토리는 무시
echo "$file\n";
$file_full_path = $dirname.DIRECTORY_SEPARATOR.$file;
if (is_file($file_full_path)) $counter_file++;
if (is_dir($file_full_path)) $counter_dir++;
}
printf("\n\n\t[ %d 개의 파일 / %d 개의 디렉토리 ]\n", $counter_file, $counter_dir);
closedir($dh);
?>
$dirname = ".";
$counter_file = 0;
$counter_dir = 0;
if (!$dh = @opendir($dirname)) {
fwrite(STDERR, "에러: 디렉토리를 열 수 없습니다.");
exit(1);
}
while (($file = readdir($dh)) !== false) {
if ($file == "." || $file == "..") continue; // . 과 .. 디렉토리는 무시
echo "$file\n";
$file_full_path = $dirname.DIRECTORY_SEPARATOR.$file;
if (is_file($file_full_path)) $counter_file++;
if (is_dir($file_full_path)) $counter_dir++;
}
printf("\n\n\t[ %d 개의 파일 / %d 개의 디렉토리 ]\n", $counter_file, $counter_dir);
closedir($dh);
?>
실행 결과 화면:
D:\Z\000>php dir.php
camera_nikon_tote_bag_ct2bk.jpg
CSC_0097.JPG
dir.php
DSCN0428.jpg
example.obj
example.php
example1.php
example2.php
test.jpg
test.zip
새 텍스트 문서.txt
새 폴더
새 폴더 (2)
[ 11 개의 파일 / 2 개의 디렉토리 ]
D:\Z\000>
camera_nikon_tote_bag_ct2bk.jpg
CSC_0097.JPG
dir.php
DSCN0428.jpg
example.obj
example.php
example1.php
example2.php
test.jpg
test.zip
새 텍스트 문서.txt
새 폴더
새 폴더 (2)
[ 11 개의 파일 / 2 개의 디렉토리 ]
D:\Z\000>
☞ PHP
감사합니다. 많은 도움이 됐습니다.^^
Post a Comment
<< Home