Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem fileperms() Function



The PHP Filesystem fileperms() function is used to give permission for a file or directory. This function can return the permission as a number on success or false on failure.

As this function returns an integer with the permissions. It can be difficult to directly understand the format in which this integer sends the permissions to the file. You can alter it, if required in a format that is easy for people to read.

Syntax

Below is the syntax of the PHP Filesystem fileperms() function −

int fileperms ( string $filename )

Parameters

The parameters are needed to use the fileperms() function are mentioned below −

Sr.No Parameter & Description
1

filename(Required)

It is the name of the file you want to check.

Return Value

It returns the permissions as an integer, or FALSE on failure.

PHP Version

All versions of PHP support the fileperms() method starting with 4.0.0. This shows that it has a lot of support and is compatible with almost all PHP configurations.

Example

The below PHP code is used to show the basic usage of PHP Filesystem fileperms() function. It is helpful for more understandable file permission verification and is mainly used on Unix-like systems.

Here the sprintf() method is used to format the integer that fileperms() produces into an octal number. And the -4 parameter shows that the last four characters of the octal string are used.

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

Output

This will produce the following result −

0666

Example

The program changes the file type and permissions using the fileperms() function into a text that can be read by humans after verifying the file's permissions.

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

This will generate the below result −

rrw-rw-

Example

This below PHP example is used to check the permissions of a file and displays that it is readable, writable, or executable.

<?php

   // Define the file path 
   $file = "/PhpProject/myfile.txt";

   // Get file permissions
   $perms = fileperms($file);
   
   // Check if file is readable
   if ($perms & 0x0100) {
      echo "$file is readable.<br>";
   } else {
      echo "$file is not readable.<br>";
   }
   
   // Check if file is writable
   if ($perms & 0x0080) {
      echo "$file is writable.<br>";
   } else {
      echo "$file is not writable.<br>";
   }
   
   // Check if file is executable
   if ($perms & 0x0040) {
      echo "$file is executable.<br>";
   } else {
      echo "$file is not executable.<br>";
   }
?> 

Output

This will create the below outcome −

/PhpProject/myfile.txt is not readable.
/PhpProject/myfile.txt is not writable.
/PhpProject/myfile.txt is not executable.

Example

This below PHP code shows us how we can change the permissions of a file using the fileperms() function. And the chmod() function is used to change the file's permissions to the new permissions.

<?php

// Define the file path 
$file = "/PhpProject/myfile.txt";

// Get the current permissions of the file
$perms = fileperms($file);

// Define new permissions (e.g., read and write for owner, read for group and others)
$new_perms = 0644;

// Change the permissions of the file
if (chmod($file, $new_perms)) {
    echo "Permissions of $file changed successfully.<br>";
} else {
    echo "Failed to change permissions of $file.<br>";
}
?> 

Output

This will lead to the following outcome −

Permissions of /PhpProject/myfile.txt changed successfully.

Note

By using fileperms() function the permissions are shown by octal notation, which can be converted into a human-readable format if it is needed.

Summary

The fileperms() can be used to get a file's or directory's permissions. The permissions are represented by an integer that is returned in octal notation. By reading the returned integer, you can identify the file type as well as the read, write, and execute permission for the owner, group, and others.

php_function_reference.htm
Advertisements