275 of 410 menu

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

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

Example

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

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

Code execution result:

true

Example

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

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

Code execution result:

false

Example

Set permissions 0755 for a directory:

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

Code execution result:

true

See Also

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