
- 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 chmod() Function
The PHP Filesystem chmod() function can change permissions of a specified file. As the name suggests change mode which means it tries to change the mode of a file specified. It returns true on success, otherwise false on failure.
Important Points
- The mode is not automatically assumed to an octal value, so strings (such as "g+w") can not work properly. To ensure the expected operation, we need to prefix mode with zero (0).
- The parameter "mode" consists of three octal number components: access restrictions for the owner, user group in which the owner is in, and everybody else in this order.
- Number 1 means that we grant execute permissions, number 2 means that we make the file writeable, and number 4 means that we make the file readable. We can add these numbers to specify the needed rights.
Syntax
Below is the syntax of the PHP Filesystem chmod() function −
bool chmod ( string filename, int permissions )
Parameters
Here are the required and optional parameters of the chmod() function −
Sr.No | Parameter & Description |
---|---|
1 |
filename(Required) The path to the file. |
2 |
permissions(Required) It is the permission that you want to change. Octal notation and symbolic notation are the two ways permission can be given. |
Return Value
It returns TRUE value on success, or FALSE on failure.
Permission Representation
There are two ways to give permission −
- Octal Notation
- Symbolic Permission
Octal Notation
Octal notation is a way to express Unix permission. It basically consists of three digits, like 777 or 775. Within it - The owner is the first number, the group is the second, and the others have the third.
Using 0 to 7 digits, this octal number system is as follows −
- 0 indicates "No" permission,
- 1 indicates "Execute" permission,
- 2 indicates "Write" permission,
- 4 indicates "Read" permission,
To provide a user class permissions, add up the numbers above. For example, add 4+1 = 5 to grant a user group only "Read" and "Execute" permissions. Let us see some examples below −
- 0 (0+0+0) for the permission "No".
- 1 (0+0+1) for permission to "Execute".
- 2 (0+2+0) for permission to "Write."
- 3 (0+2+1) for the permissions to "Write" and "Execute".
- 4 (4+0+0) for permission to "Read".
- 5 (4+0+1) for the permissions to "Read" and "Execute".
- 6 (4+2+0) for the permissions to "Read" and "Write".
- 7 (4+2+1) for the permissions "Read," "Write," and "Execute".
Some of the octal notation permissions are −
- 775: This gives the owner and the group read, write, and execute permissions, and it gives the others read and execute permissions for the given file or folder.
- 700: This gives the owner read, write, and execute permission while denying others and groups access.
Symbolic Permission
Permissions are represented in the symbolic permission method as follows −
- r: Read permission,
- w: Write permission,
- e: Execute permission.
The user classes are further represented by −
- u: owner (user) of File or Folder.
- g: group with identical permissions.
- o: All other users (apart from the owner and group).
- a: it includes all (u, g, and o).
We use below operations to provide the above permission to the user classes −
- + : add access,
- - : remove access,
- = : set exact access.
PHP Version
The chmod() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
In the below code we have used PHP Filesystem chmod() function to set a permission 0600 which provides read and write for the owner, nothing to everybody else.
<?php // File path $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile1.txt'; // Owner can read and write, others have no permissions $permissions = 0600; if (chmod($file, $permissions)) { echo "Permissions has changed successfully for myfile1.txt."; } else { echo "Failed to change permissions for myfile1.txt."; } ?>
Output
This will produce the following result −
-
If permission is granted.
Permissions has changed successfully for myfile1.txt.
-
If permission is denied.
Failed to change permissions for myfile1.txt.
Example
In the given example we will use chmod() function to set a permission 0644 which provide read and write for owner and read-only for others.
<?php // File path $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile2.txt'; // read and write for owner and read-only for others $permissions = 0644; if (chmod($file, $permissions)) { echo "Permissions has changed successfully for myfile2.txt."; } else { echo "Failed to change permissions for myfile2.txt."; } ?>
Output
This will generate the below output −
-
If permission is granted.
Permissions has changed successfully for myfile2.txt.
-
If permission is denied.
Failed to change permissions for myfile2.txt.
Example
In the below example we have used chmod() function to set a permission 0755 which provide everything for the owner and read and execute for others. Because there are some conditions where the owner need to provide the read and execute ownership to others.
<?php // File path $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile3.txt'; // everything for the owner and read and execute for others $permissions = 0755; if (chmod($file, $permissions)) { echo "Permissions has changed successfully for myfile3.txt."; } else { echo "Failed to change permissions for myfile3.txt."; } ?>
Output
This will produce the following outcome −
-
If permission is granted.
Permissions has changed successfully for myfile3.txt.
-
If permission is denied.
Failed to change permissions for myfile3.txt.
Example
In the below example we have used chmod() function to set a permission 0740 which provide everything for owner, read for owner's group, and no permissions for others.
<?php // File path $file = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile4.txt'; // everything for owner, read for owner's group, and no permissions for others $permissions = 0740; if (chmod($file, $permissions)) { echo "Permissions has changed successfully for myfile4.txt."; } else { echo "Failed to change permissions for myfile4.txt."; } ?>
Output
The result of this PHP code is −
-
If permission is granted.
Permissions has changed successfully for myfile4.txt.
-
If permission is denied.
Failed to change permissions for myfile4.txt.
Note
If there is a problem, the function may or may not be able to modify the file or folder's permissions. So, use a conditional statement to make sure the success of your code and to keep it safe.
Summary
The built-in PHP Filesystem function chmod() checks that the directories and files in your web applications are safe and have the right permissions.