tags:

views:

23

answers:

2

hey guys, i have a propably rather simple question: I'm using the following script to read a folder?

    $count = 0;
if ($handle = opendir(PATH)) {
    $retval = array();
    while (false !== ($file = readdir($handle))) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db') {
            $retval[$count] = $file;
            $count = $count + 1;
        } else {
            //no proper file
        }
    }
    closedir($handle);
}

if a file is an image I print it as an image: print "";

However i wonder how i can display FOLDERS? If i have a subfolder inside of the folder i'm currently running through? how can i print that one?

A: 

Use scandir instead of readdir. http://us.php.net/manual/en/function.scandir.php

Borealid
any chance you can show me how i can transform my code above to work with scandir? in my case i'm always looking for the file-extension. so if $ext = pathinfo($value, PATHINFO_EXTENSION); equals e.g. jpg or gif i'm printing out an image. how can i print out a folder?
Use `is_dir` to determine if the path is a directory. The only change your code needs to work with `scandir` is to call `scandir` once and then loop over its output, instead of calling `readdir` repeatedly. Unless you want recursion, in which case you'll have to implement that by changing your code a wee bit more.
Borealid
+1  A: 

use glob instead of scandir

$dirs = glob(PATH."/*", GLOB_ONLYDIR);
$images = glob(PATH."/*.[jJ][pJ][gG]");

foreach ($dirs as $name) echo "<b>$name</b><br>\n";
foreach ($images as $name) echo $name."<br>\n";
Col. Shrapnel
@Col. Shrapnel: Why? He already has the parent directory handle...
Borealid
@Borealid because glob is 10 times shorter
Col. Shrapnel