We now have a youtube channel. Subscribe!

PHP | fileperms() Function

PHP fileperms() Function

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP fileperms() Function.

The fileperms() function returns permission for a file or directory.


Following below is the syntax to use this function -

int fileperms ( string $filename )

Return Value

This PHP function returns permission as a number on success or false on failure.


Try out the below example -

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


When the above code is executed, it will produce the following result -



Try out the below example -

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

   switch($perms & 0xF000) {
      case 0xC000: // socket
         $info = 's';
      case 0xA000: // symbolic link
         $info = 'l';
      case 0x8000: // regular
         $info = 'r';
      case 0x6000: // block special
         $info = 'b';
      case 0x4000: // directory
         $info = 'd';
      case 0x2000: // character special
         $info = 'c';
      case 0x1000: // FIFO pipe
         $info = 'p';
      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;


When the above code is executed, it will produce the following result -


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP filesize() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain