디렉토리의 모든 파일을 살펴 보려고합니다. 디렉토리가 있으면 더 이상 이동할 디렉토리가 없을 때까지 모든 파일을 살펴 봅니다. 처리 된 각 항목은 아래 함수의 결과 배열에 추가됩니다. 내가 무엇을 할 수 있는지 / 내가 뭘 잘못했는지 확실하지 않지만 작동하지 않지만 아래 코드가 처리되면 브라우저가 엄청나게 느리게 실행됩니다. 어떤 도움을 주시면 감사하겠습니다!
암호:
function getDirContents($dir){
$results = array();
$files = scandir($dir);
foreach($files as $key => $value){
if(!is_dir($dir. DIRECTORY_SEPARATOR .$value)){
$results[] = $value;
} else if(is_dir($dir. DIRECTORY_SEPARATOR .$value)) {
$results[] = $value;
getDirContents($dir. DIRECTORY_SEPARATOR .$value);
}
}
}
print_r(getDirContents('/xampp/htdocs/WORK'));
답변
당신이있을 때 함수를 호출하지 않는 디렉토리의 모든 파일과 폴더를 가져 오기 .
나 ..
.
코드 :
<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
var_dump(getDirContents('/xampp/htdocs/WORK'));
출력 (예) :
array (size=12)
0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
1 => string '/xampp/htdocs/WORK/index.html' (length=29)
2 => string '/xampp/htdocs/WORK/js' (length=21)
3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
11 => string '/xampp/htdocs/WORK/styles.less' (length=30)
답변
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/folder'));
$files = array();
foreach ($rii as $file) {
if ($file->isDir()){
continue;
}
$files[] = $file->getPathname();
}
var_dump($files);
경로가있는 모든 파일을 가져옵니다.
답변
더 짧은 버전입니다.
function getDirContents($path) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = array();
foreach ($rii as $file)
if (!$file->isDir())
$files[] = $file->getPathname();
return $files;
}
var_dump(getDirContents($path));
답변
모든 파일 가져 오기 필터 (2 인자) 당신이 때 디렉토리 및 폴더를, 함수를 호출하지 않는 .
나 ..
.
코드 :
<?php
function getDirContents($dir, $filter = '', &$results = array()) {
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if(!is_dir($path)) {
if(empty($filter) || preg_match($filter, $path)) $results[] = $path;
} elseif($value != "." && $value != "..") {
getDirContents($path, $filter, $results);
}
}
return $results;
}
// Simple Call: List all files
var_dump(getDirContents('/xampp/htdocs/WORK'));
// Regex Call: List php files only
var_dump(getDirContents('/xampp/htdocs/WORK', '/\.php$/'));
출력 (예) :
// Simple Call
array(13) {
[0]=> string(69) "/xampp/htdocs/WORK.htaccess"
[1]=> string(73) "/xampp/htdocs/WORKConverter.php"
[2]=> string(69) "/xampp/htdocs/WORKEvent.php"
[3]=> string(70) "/xampp/htdocs/WORKdefault_filter.json"
[4]=> string(68) "/xampp/htdocs/WORKdefault_filter.xml"
[5]=> string(80) "/xampp/htdocs/WORKCaching/ApcCache.php"
[6]=> string(84) "/xampp/htdocs/WORKCaching/CacheFactory.php"
}
// Regex Call
array(13) {
[0]=> string(69) "/xampp/htdocs/WORKEvent.php"
[1]=> string(73) "/xampp/htdocs/WORKConverter.php"
[2]=> string(80) "/xampp/htdocs/WORKCaching/ApcCache.php"
[3]=> string(84) "/xampp/htdocs/WORKCaching/CacheFactory.php"
}
제임스 카메론의 제안.
답변
추악한 “foreach”제어 구조가없는 내 제안은
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$allFiles = array_filter(iterator_to_array($iterator), function($file) {
return $file->isFile();
});
다음 방법으로 파일 경로를 추출 할 수 있습니다.
array_keys($allFiles);
여전히 4 줄의 코드이지만 루프 나 무언가를 사용하는 것보다 더 간단합니다.
답변
숨겨진 파일과 디렉토리를 무시하고 디렉토리 내용을 배열로 얻으려는 경우 도움이 될 수 있습니다.
function dir_tree($dir_path)
{
$rdi = new \RecursiveDirectoryIterator($dir_path);
$rii = new \RecursiveIteratorIterator($rdi);
$tree = [];
foreach ($rii as $splFileInfo) {
$file_name = $splFileInfo->getFilename();
// Skip hidden files and directories.
if ($file_name[0] === '.') {
continue;
}
$path = $splFileInfo->isDir() ? array($file_name => array()) : array($file_name);
for ($depth = $rii->getDepth() - 1; $depth >= 0; $depth--) {
$path = array($rii->getSubIterator($depth)->current()->getFilename() => $path);
}
$tree = array_merge_recursive($tree, $path);
}
return $tree;
}
결과는 다음과 같습니다.
dir_tree(__DIR__.'/public');
[
'css' => [
'style.css',
'style.min.css',
],
'js' => [
'script.js',
'script.min.js',
],
'favicon.ico',
]
답변
여기에 내가 생각해 낸 것이 있는데 이것은 코드 줄이 많지 않습니다.
function show_files($start) {
$contents = scandir($start);
array_splice($contents, 0,2);
echo "<ul>";
foreach ( $contents as $item ) {
if ( is_dir("$start/$item") && (substr($item, 0,1) != '.') ) {
echo "<li>$item</li>";
show_files("$start/$item");
} else {
echo "<li>$item</li>";
}
}
echo "</ul>";
}
show_files('./');
다음과 같이 출력됩니다.
..idea
.add.php
.add_task.php
.helpers
.countries.php
.mysqli_connect.php
.sort.php
.test.js
.test.php
.view_tasks.php
** 점은 정렬되지 않은 목록의 점입니다.
도움이 되었기를 바랍니다.
