Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

chmod

Description

The chmod of Filesystem for PHP changes file mode.

Syntax

chmod(
    string $filename,
    int $permissions
): bool

Parameters

filename

Path to the file.

permissions

NOTE: permissions is not automatically assumed to be an octal value, so to ensure the expected operation, you need to prefix permissions with a zero (0). Strings such as "g+w" will not work properly.

<?

// string, incorrect
chmod("/somedir/somefile", "u+rwx,go+rx");

// decimal, probably incorrect
chmod("/somedir/somefile", 755);

// octal, correct value of mode
chmod("/somedir/somefile", 0755);

?>

The permissions parameter consists of three octal number components specifying access restrictions for the owner, the user group in which the owner is in, and to everybody else in this order. One component can be computed by adding up the needed permissions for that target user base. Number 1 means that you grant execute rights, number 2 means that you make the file writeable, number 4 means that you make the file readable. Add up these numbers to specify needed rights. You can also read more about modes on Unix systems with 'man 1 chmod' and 'man 2 chmod'.

<?

// 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);

?>

Return

Returns true on success or false on failure.

Examples

1 · filename permissions

<?

$format = "%o";
$start = -4;

$filename = "/tmp";
$permissions1 = 0600;
$permissions2 = 0700;

clearstatcache();
chmod($filename, $permissions1);

echo substr(sprintf($format, fileperms($filename)), $start) . PHP_EOL;

clearstatcache();
chmod($filename, $permissions2);

echo substr(sprintf($format, fileperms($filename)), $start);

?>
'neutral'
HomeMenu