
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.