PHP fileperms() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fileperms() returns the permissions of a file or folder as a number.

The number holds both the file type and the permission bits. To see the familiar values such as 0644 or 0755, format the result as octal and keep the last four digits.

Syntax

Call fileperms() with the values you need. Start with this line.

PHP
$perms = fileperms('notes.txt');
CallWhat it does
fileperms('notes.txt')Returns the full permission value, for example 33188.
substr(sprintf('%o', fileperms('notes.txt')), -4)Shows the permissions in the usual form, such as 0644.
fileperms('notes.txt') & 0777Keeps only the permission bits and drops the file type bits.

On Windows, permissions are very limited, so you will mostly see values like 0666 for normal files and 0777 for folders. On Linux and macOS the value matches what you see with the ls -l command.

Library declarations
PHP
fileperms(string $filename): int|false

Parameters

ParameterDescription
filenameRequired. The path to the file or folder you want to check.

Return Value

Return ValueDescription
intReturns the permissions as an integer that includes the file type bits.
falseReturns false on failure, for example when the file does not exist.

PHP Examples

Example 1 — Show Permissions as Octal

Set a file to 0644 and read the permissions back in the usual format.

PHP
<?php
$file = __DIR__ . '/perm_demo.txt';
file_put_contents($file, 'data');
chmod($file, 0644);

$perms = fileperms($file);

echo 'Octal: ', substr(sprintf('%o', $perms), -4), PHP_EOL;

unlink($file);

How It Works

  1. chmod() sets the file to 0644, which means the owner can read and write while everyone else can only read.
  2. fileperms() returns a big number that includes both the type and the permissions.
  3. sprintf('%o') prints that number in octal, and substr() keeps the last four digits.
  4. The result 0644 matches the value we set.

Example 2 — A Missing File

fileperms() returns false when the path does not exist.

PHP
<?php
$file = __DIR__ . '/missing.txt';

$perms = @fileperms($file);

if ($perms === false) {
    echo "File not found, no permissions to read", PHP_EOL;
} else {
    echo substr(sprintf('%o', $perms), -4), PHP_EOL;
}

How It Works

  1. missing. txt is not in the folder.
  2. fileperms() returns false and the @ sign hides the warning.
  3. The script checks for false before it formats anything.
  4. Skipping that check would print a confusing value like 0 for a file that does not exist.

Example 3 — Check If a File Is Private

Use a bit mask to test whether other users are allowed to read the file.

PHP
<?php
$file = __DIR__ . '/secret.txt';
file_put_contents($file, 'top secret');
chmod($file, 0600);
clearstatcache();

$perms = fileperms($file);

echo 'Octal: ', substr(sprintf('%o', $perms), -4), PHP_EOL;
echo 'Readable by everyone: ', ($perms & 0004) ? 'yes' : 'no', PHP_EOL;

unlink($file);

How It Works

  1. chmod() sets 0600, so only the owner can read and write the file.
  2. clearstatcache() makes sure PHP reads the new permissions.
  3. The bit mask 0004 is the permission for others to read.
  4. The & operator finds that bit is off, so the answer is no.

Frequently Asked Questions

It returns the permissions of a file or folder as an integer, including the file type bits.
Use substr(sprintf('%o', fileperms($file)), -4). It formats the number in octal and keeps the last four digits.
fileperms() reads the current permissions, while chmod() changes them.
It works but only returns simple values, such as 0666 for files and 0777 for folders, because Windows does not use Unix permissions.
PHP caches file information. Call clearstatcache() after chmod() and before fileperms() to get the fresh value.

Next: filesize()

filesize() returns the size of a file in bytes.

filesize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful