tags:

views:

46

answers:

5

Hi I am trying to show all files and folders in a dir with php

e.g

Dir: system/infomation/

Folder - User

Files From User - User1.txt

Files From User - User2.txt

Files From User - User3.txt

Files From User - User4.txt

Folder - Players

Files From Players - Player1.txt

Files From Players - Player2.txt

Files From Players - Player3.txt

Files From Players - Player4.txt

Can someone lead me down the right street please

Thank You

A: 

You may use Directory Functions: http://php.net/manual/en/book.dir.php

Simple example from opendir() function description:

<?php
$dir_path = "/path/to/your/dir";

if (is_dir($dir_path)) {
    if ($dir_handler = opendir($dir_path)) {
        while (($file = readdir($dir_handler)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir_path . $file) . "\n";
        }
        closedir($dir_handler);
    }
}
?>
Kel
A: 

Use the Dir class or opendir() and readdir() in a recursive function.

http://www.php.net/manual/en/class.dir.php

http://ch2.php.net/manual/en/function.opendir.php

http://ch2.php.net/manual/en/function.readdir.php

joni
+4  A: 

PHP 5 has the RecursiveDirectoryIterator.

The manual has a basic example:

<?php

$directory = '/system/infomation/';

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

while($it->valid()) {

    if (!$it->isDot()) {

        echo 'SubPathName: ' . $it->getSubPathName() . "\n";
        echo 'SubPath:     ' . $it->getSubPath() . "\n";
        echo 'Key:         ' . $it->key() . "\n\n";
    }

    $it->next();
}

?>

Edit -- Here's a slightly more advanced example (only slightly) which produces output similar to what you want (i.e. folder names then files).

// Create recursive dir iterator which skips dot folders
$dir = new RecursiveDirectoryIterator('./system/information',
    FilesystemIterator::SKIP_DOTS);

// Flatten the recursive iterator, folders come before their files
$it  = new RecursiveIteratorIterator($dir,
    RecursiveIteratorIterator::SELF_FIRST);

// Maximum depth is 1 level deeper than the base folder
$it->setMaxDepth(1);

// Basic loop displaying different messages based on file or folder
foreach ($it as $fileinfo) {
    if ($fileinfo->isDir()) {
        printf("Folder - %s\n", $fileinfo->getFilename());
    } elseif ($fileinfo->isFile()) {
        printf("File From %s - %s\n", $it->getSubPath(), $fileinfo->getFilename());
    }
}
Pekka
+1 for making answer to an obvious duplicate a CW
Gordon
Would there be any chance you can make it so all the files that belongs to that folder will list under that one folder name?
Gully
@Gully yes, using `basename()` and `dirname()`
Pekka
@Gully see the 2nd example code snippet
salathe
A: 

You can use:

 foreach (new DirectoryIterator("./system/information/") as $fn) {
     print $fn->getFilename();
 }

You'll have to use it twice for each subdir, Players and User.

mario
A: 

I've found in www.laughing-buddha.net/php/lib/dirlist/ a function that returns an array containing a list of a directory's contents.

Also look at php.net http://es.php.net/manual/es/ref.filesystem.php where you'll find additional functions for working with files in php.

jLuengas