Working with Directories : is_dir « File Directory « PHP






Working with Directories

 
resource opendir ( string path ) 
bool is_dir ( string path ) 
string readdir ( resource dir_handle ) 
void closedir ( resource dir_handle ) 
array scandir ( string directory [, int sorting_order [, resource context]] ) 

<?php 

function numfilesindir ($thedir){ 
    if (is_dir ($thedir)){ 
        $scanarray = scandir ($thedir); 
        for ($i = 0; $i < count ($scanarray); $i++){ 
            if ($scanarray[$i] != "." && $scanarray[$i] != ".."){ 
                if (is_file ($thedir . "/" . $scanarray[$i])){ 
                    echo $scanarray[$i] . "<br />"; 
                } 
            } 
        } 
    } else { 
        echo "Sorry, this directory does not exist."; 
    } 
} 
echo numfilesindir ("sample1"); 

?> 


<?
    $handle = opendir('/path/to/directory')

    if ($handle) {
            while (false !=  = ($file = readdir($handle))) {
                    print "$file<br />\n";
            }
            closedir($handle);
    }
?>
  
  








Related examples in the same category

1.is_dir() function verifies that the file is a directory
2.Creating a Function That Performs Multiple File Tests
3.Is a directory