Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ดึง Subdirectories ทั้งหมดของโฟลเดอร์ใน PHP

หลายวิธีในการดึงรายการ Subdirectory ของโฟลเดอร์ใน PHP เลือกใช้ตาม Version และความต้องการ

วิธีที่ 1: glob() + GLOB_ONLYDIR (แนะนำ, เรียบง่าย)

$path = '/var/www/html';

$dirs = glob($path . '/*', GLOB_ONLYDIR);
// หรือ
$dirs = glob($path . '/*', GLOB_ONLYDIR | GLOB_NOSORT); // ไม่ Sort เร็วกว่า

print_r($dirs);
/* ผลลัพธ์:
Array
(
    [0] => /var/www/html/css
    [1] => /var/www/html/js
    [2] => /var/www/html/images
)
*/

วิธีที่ 2: scandir() + is_dir() (รองรับทุก Version, ควบคุมได้ละเอียด)

function getSubdirectories($path) {
    if (!is_dir($path)) return [];
    
    $entries = scandir($path);
    $dirs = [];
    
    foreach ($entries as $entry) {
        // ข้าม . และ ..
        if ($entry === '.' || $entry === '..') continue;
        
        $fullPath = $path . DIRECTORY_SEPARATOR . $entry;
        if (is_dir($fullPath)) {
            $dirs[] = $fullPath; // หรือ $entry ถ้าต้องแค่ชื่อ
        }
    }
    
    return $dirs;
}

$dirs = getSubdirectories('/var/www/html');

วิธีที่ 3: DirectoryIterator (OOP, Memory Efficient, PHP 5+)

$iterator = new DirectoryIterator('/var/www/html');
$dirs = [];

foreach ($iterator as $fileInfo) {
    if ($fileInfo->isDir() && !$fileInfo->isDot()) {
        $dirs[] = $fileInfo->getPathname(); // Full Path
        // หรือ $fileInfo->getFilename() สำหรับแค่ชื่อ
    }
}

// หรือแบบ One-liner (PHP 5.4+)
$dirs = array_map(
    fn($f) => $f->getPathname(),
    array_filter(
        new DirectoryIterator('/var/www/html'),
        fn($f) => $f->isDir() && !$f->isDot()
    )
);

วิธีที่ 4: RecursiveDirectoryIterator (Recursive - รวม Sub-sub-directories)

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/var/www/html', RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST
);

$dirs = [];
foreach ($iterator as $fileInfo) {
    if ($fileInfo->isDir()) {
        $dirs[] = $fileInfo->getPathname();
    }
}

วิธีที่ 5: glob() Recursive (PHP 5.5+)

$dirs = glob('/var/www/html/**/*', GLOB_ONLYDIR | GLOB_BRACE);
// ต้องมี ** และ GLOB_BRACE (ไม่รองรับบน Windows บาง Version)

Benchmark (โฟลเดอร์ 1000+ Subdirectories)

/* ผลลัพธ์ประมาณ:
glob(GLOB_ONLYDIR):           0.0012s
scandir + is_dir:             0.0015s
DirectoryIterator:            0.0018s
RecursiveDirectoryIterator:   0.0025s (Recursive)
*/
// glob() เร็วที่สุดและ Code สั้นที่สุดสำหรับ Non-recursive

กรองตาม Pattern (เช่น เฉพาะโฟลเดอร์ขึ้นต้นด้วย "app_")

$dirs = glob('/var/www/html/app_*', GLOB_ONLYDIR);
// หรือ
$dirs = array_filter(glob('/var/www/html/*', GLOB_ONLYDIR), function($path) {
    return strpos(basename($path), 'app_') === 0;
});

⚠️ ข้อควรระวัง

  • glob() คืนค่า false ถ้า Error (ไม่ใช่ Empty Array) - ต้อง Check
  • Symlink: is_dir() คืน true สำหรับ Symlink ไปโฟลเดอร์ ใช้ is_link() เช็คแยก
  • Permission: ต้องมี Read Permission บนโฟลเดอร์
  • Windows: Path Separator ใช้ DIRECTORY_SEPARATOR หรือ / (PHP รองรับทั้งคู่)