PHP chmod Function: Changing File Permissions
The PHP chmod function changes a file's permission mode by specifying the file path and an octal mode value, returning true on success and false on failure, with detailed parameter descriptions and multiple code examples illustrating correct and incorrect usage.
The chmod function in PHP changes the mode (permissions) of a specified file.
Signature: chmod(string $filename, int $mode): bool
Description: Attempts to set the file mode of $filename to the value given by $mode . The mode must be provided as an octal integer (e.g., 0755 ); strings such as "g+w" are not accepted.
Parameters:
filename – the path to the file.
mode – the permission bits; must be an octal integer, prefixed with 0 .
Return value: Returns TRUE on success, FALSE on failure.
Examples:
Example 1 (incorrect decimal mode, correct octal mode):
<?php
chmod("/somedir/somefile", 755); // decimal, may be wrong
chmod("/somedir/somefile", "u+rwx,go+rx"); // string, wrong
chmod("/somedir/somefile", 0755); // correct octal
?>Example 2 (various permission settings):
<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);
// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.