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
| Function | Purpose |
|---|---|
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
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: Use octal permissions such as 0640, not decimal 640 | Use 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.