scandir() for PHP4

A function that lets you use scandir() in versions prior to PHP5.

This function is very similar to PHP5’s scandir(), except that it does not support the $context parameter. To sort in descending order , set $sorting_order to 1.

<?php
if( !function_exists('scandir') ) {
    function scandir($directory, $sorting_order = 0) {
        $dh  = opendir($directory);
        while( false !== ($filename = readdir($dh)) ) {
            $files[] = $filename;
        }
        if( $sorting_order == 0 ) {
            sort($files);
        } else {
            rsort($files);
        }
        return($files);
    }
}
?>

The purpose of this function is to enable the use of scandir() in PHP versions prior to PHP5. It is “future compatible”, so you don’t have to worry about anything breaking when you upgrade to PHP5.

Comments

Deceptively similar to http://php.net/scandir

Dan.

#1 Daniel on Mar 28th, 2008

Could you post the code what it would look like if you used the opendir() function instead of the scandir() function in the jqueryFileTree.php file from you previous article ?
I'm new to scripting and can't seem to get it done...
Your help is much appreciated !

#2 John on Mar 28th, 2008

John, see Example #1 at http://php.net/opendir

#3 Cory S.N. LaViska on Mar 28th, 2008

Add a comment

Name*

Email*

Never, ever sold or spammed :)

Homepage

Comment*

Sorry, plain text only :(