PHP | DirectoryIterator valid() Function
The DirectoryIterator::valid() function is an inbuilt function in PHP which is used to check the current DirectoryIterator position is a valid file or not.
Syntax:
bool DirectoryIterator::valid( void )
Parameters: This function does not accept any parameters.
Return Value: This function returns TRUE if the position is valid, otherwise returns FALSE.
Below programs illustrate the DirectoryIterator::valid() function in PHP:
Program 1:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
$directory ->seek(2);
if ( $directory ->valid()) {
echo $directory ->getFilename();
}
?>
|
Output:
applications.html
Program 2:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
while ( $directory ->valid()) {
if ( $directory ->isDir()) {
echo $directory ->key() . " => " .
$directory ->getFilename() . "<br>" ;
}
$directory ->next();
}
?>
|
Output:
0 => .
1 => ..
4 => dashboard
8 => img
11 => webalizer
12 => xampp
Note: The output of this function depends on the content of server folder.
Reference: https://www.php.net/manual/en/directoryiterator.valid.php
Last Updated :
26 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...