• PHP Video Tutorials

PHP - Function fileperms()



The fileperms() function can return permission for a file or directory. This function can return the permission as a number on success or false on failure.

Syntax

int fileperms ( string $filename )

Example-1

<?php
   echo substr(sprintf("%o", fileperms("/PhpProject/sample.txt")), -4);
?>

Output

0666

Example-2

<?php
   $perms = fileperms("/PhpProject/sample.txt");

   switch($perms & 0xF000) {
      case 0xC000: // socket
         $info = 's';
         break;
      case 0xA000: // symbolic link
         $info = 'l';
         break;
      case 0x8000: // regular
         $info = 'r';
         break;
      case 0x6000: // block special
         $info = 'b';
         break;
      case 0x4000: // directory
         $info = 'd';
         break;
      case 0x2000: // character special
         $info = 'c';
         break;
      case 0x1000: // FIFO pipe
         $info = 'p';
         break;
      default: // unknown
         $info = 'u';
   }

   // Owner
   $info .= (($perms & 0x0100) ? 'r' : '-');
   $info .= (($perms & 0x0080) ? 'w' : '-');
   $info .= (($perms & 0x0040) ?
            (($perms & 0x0800) ? 's' : 'x' ) :
            (($perms & 0x0800) ? 'S' : '-'));

   // Group
   $info .= (($perms & 0x0020) ? 'r' : '-');
   $info .= (($perms & 0x0010) ? 'w' : '-');
   $info .= (($perms & 0x0008) ?
            (($perms & 0x0400) ? 's' : 'x' ) :
            (($perms & 0x0400) ? 'S' : '-'));

   echo $info;
?>

Output

rrw-rw-
php_function_reference.htm
Advertisements