scandir() for PHP4

A note from A Beautiful Site’s founder: We developed Surreal to be easy for you and your clients. If you’re a web designer, you should take a look at the simple, hosted CMS that’s changing the way content is managed. Surreal integrates in moments and is trusted by over 18,000 websites. Try it out for free and let us know what you think! Visit Website »

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.

If you enjoyed this article, please share it with a friend!

4 Responses to scandir() for PHP4

  1. Daniel says:

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

    Dan.

  2. John says:

    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 !

  3. Stephan says:

    I owe you a drink or two! Thanks a million.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>