PHP fileowner() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fileowner() returns the user id of the owner of a file.

The result is a number, not a name. On Linux and macOS you can turn that number into a user name with posix_getpwuid() if the POSIX extension is installed.

Syntax

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

PHP
$uid = fileowner('notes.txt');
CallWhat it does
fileowner('notes.txt')Returns the numeric user id that owns the file, such as 1000 or 33.
posix_getpwuid(fileowner('notes.txt'))['name']Looks up the owner's user name from the id.
fileowner('missing.txt')Returns false and raises a warning because the file does not exist.

On Windows, fileowner() always returns 0 because owners work differently there. Results are cached, so call clearstatcache() if the owner may have changed during the script.

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

Parameters

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

Return Value

Return ValueDescription
intReturns the numeric user id of the owner.
falseReturns false on failure, for example when the file does not exist.

PHP Examples

Example 1 — Read the Owner Id

Create a file and print the user id of its owner.

PHP
<?php
$file = __DIR__ . '/owner_demo.txt';
file_put_contents($file, 'Hello');

$uid = fileowner($file);

echo "Owner ID: $uid", PHP_EOL;

unlink($file);

How It Works

  1. file_put_contents() creates the file, so it belongs to the user running PHP.
  2. fileowner() returns the numeric id of that user.
  3. The id is printed. On a web server it is often the web user, such as 33 for www-data.
  4. unlink() removes the demo file.

Example 2 — A File That Does Not Exist

fileowner() returns false for a missing file, and 0 is a valid owner id, so use a strict check.

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

$uid = @fileowner($file);

if ($uid === false) {
    echo "Cannot read the owner", PHP_EOL;
} else {
    echo "Owner ID: $uid", PHP_EOL;
}

How It Works

  1. missing. txt does not exist, so fileowner() cannot find an owner.
  2. It returns false and the @ sign hides the warning.
  3. A loose check like if (!$uid) would also catch the root user with id 0, which is a mistake.
  4. === false only matches a real failure.

Example 3 — Find the Owner Name

Convert the numeric id into a user name with the POSIX extension when it is available.

PHP
<?php
$file = __DIR__ . '/owner_name.txt';
file_put_contents($file, 'Hello');

$uid = fileowner($file);

if (function_exists('posix_getpwuid')) {
    $info = posix_getpwuid($uid);
    echo 'Owner name: ', $info['name'], PHP_EOL;
} else {
    echo 'POSIX extension is not available', PHP_EOL;
}

unlink($file);

How It Works

  1. fileowner() gives the numeric user id of the file owner.
  2. function_exists() checks that the POSIX extension is installed before it is used.
  3. posix_getpwuid() looks up the user record for that id and returns an array.
  4. The name key of the array holds the user name, such as www-data.

Frequently Asked Questions

It returns the numeric user id of the owner of a file or folder.
Pass the id to posix_getpwuid() and read the name key. This needs the POSIX extension, which is not available on Windows.
On Linux, 0 is the root user. On Windows, PHP always returns 0 because the concept of a numeric owner does not apply.
It returns false and raises a warning, for example when the file does not exist.
fileowner() returns the id of the user who owns the file, while filegroup() returns the id of the group that owns it.

Next: fileperms()

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

fileperms() 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