Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

chmod

Changes the permissions of a file or folder.

Syntax

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

Parameters

ParameterTypeDescription
$filenamestringThe path.
$permissionsintThe permissions in octal, such as `0644`.

Return value

bool`true` on success.

Examples

A file readable by the owner and the group only:

<?php
 
// setup.php
 
chmod(__DIR__ . '/config.php', 0640);
chmod(__DIR__ . '/storage', 0775);

Notes

Write the number with a leading zero: 0644, not 644. 0777 — access for everyone — is unsafe on a server. On Windows the function changes almost nothing.

See also

  • mkdir — Creates a folder.
  • is_writable — Checks whether a file or folder can be written to.
  • touch — Creates an empty file or updates its modification time.

In the tutorial

  • Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.

Thanks for reading!

Donate to the site — we work hard for you.