Scan current folder using PHP

17,136

Solution 1

$files = glob(dirname(__FILE__) . "/*.php");

http://php.net/manual/en/function.glob.php

Solution 2

foreach (scandir('.') as $file)
    echo $file . "\n";

Solution 3

From the PHP manual:

$dir = new DirectoryIterator(dirname($path));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}

Solution 4

<?php

$path = new DirectoryIterator('/articles');

foreach ($path as $file) {
    echo $file->getFilename() . "\t";
    echo $file->getSize() . "\t";
    echo $file->getOwner() . "\t";
    echo $file->getMTime() . "\n";
}

?>

From The Standard PHP Library (SPL)

Solution 5

try this

   $dir = glob(dirname(__FILE__));
   $directory = array_diff(scandir($dir[0]), array('..', '.'));
   print_r($directory);
Share:
17,136
kmunky
Author by

kmunky

Updated on June 04, 2022

Comments

  • kmunky
    kmunky almost 2 years

    I have a folder structure like this:

    /articles
         .index.php
         .second.php
         .third.php
         .fourth.php
    

    If I'm writing my code in second.php, how can I scan the current folder(articles)?

    Thanks