2010-12-30 4 views

답변

2
foreach(glob('path/to/directory/file/*') as $file) 
     echo $file,"\n"; 
} 

하거나

print_r(glob('path/to/directory/file/*')); 
+0

아주 좋지만 "path/to/directory/file /*.*"와 같은 경로 여야 만합니다. – faressoft

2

: PHP: List Contents of a Directory
는 디렉토리 경로 대신 "."를 놓습니다. 당신이 사용할 수있는

// open this directory 
$myDirectory = opendir("."); 

// get each entry 
while($entryName = readdir($myDirectory)) { 
$dirArray[] = $entryName; 
} 

// close directory 
closedir($myDirectory); 

// count elements in array 
$indexCount = count($dirArray); 
Print ("$indexCount files<br>\n"); 

// sort 'em 
sort($dirArray); 

// print 'em 
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n"); 
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n"); 
// loop through the array of files and print them all 
for($index=0; $index < $indexCount; $index++) { 
     if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files 
    print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>"); 
    print("<td>"); 
    print(filetype($dirArray[$index])); 
    print("</td>"); 
    print("<td>"); 
    print(filesize($dirArray[$index])); 
    print("</td>"); 
    print("</TR>\n"); 
} 
} 
print("</TABLE>\n"); 
1

(리눅스에서 .로 시작)는 숨겨진 파일을 표시하지 않습니다 glob

$folder = "file"; 
$mask = "*.*"; 
$files = glob("" . $folder . $mask); 
foreach ($files as $file) 
{ 
    $file_name = basename($file,substr($mask,1)); // cut the folder and extension 
    echo $file_name; 
} 
3

DirectoryIterator

$dir = new DirectoryIterator('/file/'); <-- remember to put in absolute path 
foreach ($dir as $fileinfo) 
{ 
    if (!$fileinfo->isDot() && $fileinfo->getType()!='dir') 
    { 
    var_dump($fileinfo->getFilename()); 
    } 
} 
+0

경로가 절대적 일 필요는 없습니다. 질문은 파일을 요구하기 때문에'isFile()'메소드 만 사용하여 파일을 검사하는 것으로 충분합니다. 그리고, 가치있는 것을 위해 ['FilesystemIterator'] (http://php.net/filesystemiterator)는 기본적으로 도트 파일을 건너 뜁니다. – salathe

관련 문제