Skip to content
Related Articles

Related Articles

Improve Article
PHP | DirectoryIterator getPerms() Function
  • Last Updated : 26 Nov, 2019

The DirectoryIterator::getPerms() function is an inbuilt function in PHP which is used to get the permissions of the current DirectoryIterator item.

Syntax:

int DirectoryIterator::getPerms( void )

Parameters: This function does not accept any parameters.

Return Value: This function rethrns the file permissions as a decimal integer.

Below programs illustrate the DirectoryIterator::getPerms() function in PHP:



Program 1:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
  
    // If not a dot folder
    if (!$directory->isDot()) {
        $perms = substr(sprintf('%o', $directory->getPerms()), -4);
  
        // Display the filename with permission
        echo $directory->getFilename() . " " 
            . " | Permission: " . $perms . "<br>";
    }
    $directory->next();
}
  
?>

Output:

applications.html | Permission: 0666
bitnami.css | Permission: 0666
dashboard | Permission: 0777
favicon.ico | Permission: 0666
geeks.PNG | Permission: 0666
gfg.php | Permission: 0666
img | Permission: 0777
index.php | Permission: 0666
webalizer | Permission: 0777
xampp | Permission: 0777

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    // If not a dot folder
    if (!$dir->isDot()) {
        $perms = substr(sprintf('%o', $dir->getPerms()), -4);
  
        // Display the filename with permission
        echo $dir->getFilename() . " " 
                . " | Permission: " . $perms . "<br>";
    }
}
  
?>

Output:

applications.html | Permission: 0666
bitnami.css | Permission: 0666
dashboard | Permission: 0777
favicon.ico | Permission: 0666
geeks.PNG | Permission: 0666
gfg.php | Permission: 0666
img | Permission: 0777
index.php | Permission: 0666
webalizer | Permission: 0777
xampp | Permission: 0777

Note: The output of this function depends on the content of server folder.

Reference: https://www.php.net/manual/en/directoryiterator.getperms.php




My Personal Notes arrow_drop_up
Recommended Articles
Page :