PHP4 Scandir() alternative

I was developing a website locally (php5) ans didnt realise that the scandir() function is not in php4 (the one installed on my server). Is there an alternative that I can use for php4. Here is the code I am using.


while ($month = mysql_fetch_array($do_months, MYSQL_ASSOC)) {
		  $gall_num = "gallery/".$month['name']."/";
		  $num = scandir($gall_num);
		  $total = sizeof($num) - 2;

Thanks for any help.
Harro :wink:

From PHP’s scandir page:


<?php
if(!function_exists('scandir')) {
   function scandir($dir, $sortorder = 0) {
       if(is_dir($dir))        {
           $dirlist = opendir($dir);
           while( ($file = readdir($dirlist)) !== false) {
               if(!is_dir($file)) {
                   $files[] = $file;
               }
           }
           ($sortorder == 0) ? asort($files) : rsort($files);
           return $files;
       } else {
       return FALSE;
       break;
       }
   }
}
?>

Not tested though :slight_smile:

Hi,

[fphp]opendir()[/fphp] will allow you to achieve the same thing. Check the page for some basic code to read a directory.

if you need to use a function that is not yet available in your version of php, you should consult the manual as you can usually find equivalent code either directly in the php documentation for that function, or in the user comments for that function.

another thing you can do is take a look at pears PHP_Compat
http://pear.php.net/package/PHP_Compat

you can install the entire package, or you can just grab the functions you need from the package code. they have them arranged 1 function per file so its very easy to find the one you need.

Thanks all for your help, its working now.
Thanks Harro :wink: