W3docs

fileowner()

The fileowner() function is a built-in PHP function that returns the owner of a file. This function returns the owner as an integer, which is the user ID of the

The fileowner() function is a built-in PHP function that returns the owner of a file as a numeric user ID (UID) — the same UID the operating system stores in the file's metadata. It is the PHP equivalent of looking at the owner column in ls -l. This page explains the syntax, return values, the common pitfall of stale cached results, and how to turn the numeric UID into a human-readable username.

Syntax

fileowner(string $filename): int|false
  • $filename — the path to the file (or directory) to inspect. It can be relative or absolute.
  • Returns the owner's user ID as an int on success, or false on failure (for example, if the file does not exist or the script lacks permission to read its metadata).

Because a valid UID can be 0 (the root user on Unix systems), always compare the result with the strict === false operator rather than a loose truthiness check — otherwise a perfectly valid root-owned file would look like an error.

Basic example

<?php

$filename = __FILE__; // inspect the running script itself

$owner_id = fileowner($filename);

if ($owner_id === false) {
    echo "Failed to get the owner of the file.";
} else {
    echo "The owner of '$filename' has user ID $owner_id.";
}

A possible output is:

The owner of '/var/www/example.php' has user ID 33.

The exact number depends on which OS account created or owns the file (for example, 33 is the default www-data user on many Debian/Ubuntu systems).

Converting the UID to a username

A bare number is rarely what you want to show a human. On Unix-like systems with the POSIX extension enabled, pass the UID to posix_getpwuid() to look up the account details:

<?php

$uid = fileowner(__FILE__);

if ($uid !== false && function_exists('posix_getpwuid')) {
    $info = posix_getpwuid($uid);
    echo "Owner: {$info['name']} (UID {$uid})";
} else {
    echo "Owner UID: " . var_export($uid, true);
}

This prints something like Owner: www-data (UID 33). The POSIX extension is not available on Windows, so guard the call with function_exists() as shown.

Watch out for cached results

PHP caches the results of stat-based functions (fileowner(), fileperms(), filegroup(), filesize(), and friends) for performance. If the file's owner changes during the same script run — for instance after you call chown()fileowner() may return the old value. Clear the cache first with clearstatcache():

<?php

$file = 'report.txt';

chown($file, 'nobody');     // change ownership
clearstatcache();           // discard the stale stat cache
$uid = fileowner($file);    // now reflects the new owner

Notes and gotchas

  • Windows: the concept of a numeric POSIX owner does not map cleanly to Windows ACLs. fileowner() typically returns 0, so do not rely on it for permission logic on Windows.
  • false vs. 0: 0 is root (a real owner); false means the call failed. Use ===.
  • Need more than the owner? stat() returns the full metadata array (size, mode, owner, group, timestamps) in one call, which is cheaper than calling several file*() functions separately.
  • Check access first: if you only need to know whether you can read a file, is_readable() is the right tool — fileowner() answers a different question.

Conclusion

fileowner() is a small but useful function for reading the numeric owner of a file in PHP. Compare its result with === false, remember that 0 is a valid owner (root), call clearstatcache() after changing ownership, and use posix_getpwuid() when you need the human-readable username. On Windows its result is not meaningful, so reserve owner-based logic for Unix-like systems.

Practice

Practice
What is the purpose of the fileowner() function in PHP?
What is the purpose of the fileowner() function in PHP?
Was this page helpful?