Content · Programming · PHP

PHP chmod() Function: Usage and Examples

Learn how PHP chmod works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Changes file permission bits where supported by the operating system.

What Is chmod() in PHP?

Changes file permission bits where supported by the operating system.

Its syntax is:

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

Parameters:

  • $filename — Accepted as string; required argument.
  • $permissions — Accepted as int; required argument.

Return type: bool. Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Availability: Filesystem permissions apply; Windows behavior differs.

Example 1: Basic Usage

<?php

var_dump(chmod("/tmp/report.txt", 0640));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Example 2: A Different Input

<?php

var_dump(chmod("/tmp/report.txt", 0600));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Practical Example: Applying It in a Program

<?php

$filePath = tempnam(sys_get_temp_dir(), "permissions_"); chmod($filePath, 0600); echo substr(sprintf("%o", fileperms($filePath)), -4); unlink($filePath);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Important Behavior and Edge Cases

  • Key behavior: Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.
  • Environment: Filesystem permissions apply; Windows behavior differs.

Comparison With Related Functions

FunctionPurpose
chmod()Changes file permission bits where supported by the operating system.
fileperms()fileperms() reads the permission bits; chmod() attempts to change them.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Use octal permissions such as 0640, not decimal 640Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Key Takeaways

  • Changes file permission bits where supported by the operating system.
  • Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.
  • Filesystem permissions apply; Windows behavior differs.

Summary: Use octal permissions such as 0640, not decimal 640. This does not override filesystem ownership rules.

Share this resourceSend this page to someone who may find it useful.