The chmod Function

The chmod function changes the permissions for the specified file. It accepts the file path as the first parameter and the numeric value of permissions as the second parameter. Returns true on success and false on error.

Syntax

phpchmod(string $filename, int $mode): bool

Example

Set permissions 0644 for the file 'example.txt':

php<?php
  $res = chmod('example.txt', 0644);
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's try to change permissions for a non-existent file:

php<?php
  $res = chmod('nonexistent.txt', 0644);
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Set permissions 0755 for a directory:

php<?php
  $res = chmod('mydir', 0755);
  var_dump($res);
?>

Code execution result:

phptrue

See Also

  • the chown function,
    which changes the file owner
  • the chgrp function,
    which changes the file group