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.

Syntax

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.

Example1

Try out the below example -

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

Output

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

0666

Example2

Try out the below example -

<?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

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

rrw-rw-


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.