PHP chown() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, chown() changes the owner of a file or folder. You can give the user as a name such as www-data or as a number (the user ID).

Only the super user (root) can give a file to a different owner. A normal user can only set the owner to the same user, so on shared hosting this function is often limited. It does not work on Windows.

Syntax

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

PHP
$ok = chown(__DIR__ . "/demo.txt", "www-data");
CallWhat it does
chown($file, "www-data")Changes the owner by user name.
chown($file, 33)Changes the owner by numeric user ID.
chown($file, fileowner($file))Sets the same owner again, which is a safe way to test the call.

chown() clears the file status cache for the file after changing it. On Windows it silently returns true without changing anything.

Library declarations
PHP
chown(string $filename, string|int $user): bool

Parameters

ParameterDescription
filenameRequired. The path to the file or folder.
userRequired. The user name as a string, or the user ID as an integer.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Set the Owner of a File

Create a file, read its owner ID, and set it again with chown(). Your user ID will be different.

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

$uid = fileowner($file);

if (chown($file, $uid)) {
    echo "Owner is user ID ", $uid, PHP_EOL;
}

unlink($file);

How It Works

  1. file_put_contents() creates the demo file.
  2. fileowner() returns the numeric ID of the current owner.
  3. chown() sets the owner to that same ID, which a normal user may do.
  4. unlink() deletes the demo file at the end.

Example 2 — Handle a Failed Change

If the file is missing or you lack permission, chown() returns false. Check the result instead of assuming it worked.

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

if (!@chown($file, "root")) {
    echo "Could not change the owner.", PHP_EOL;
}

How It Works

  1. The file does not exist, so the change cannot work.
  2. The @ operator hides the warning that PHP would print.
  3. chown() returns false and the if block runs.
  4. The script prints a clear message to the user.

Example 3 — Show the Owner Name

Turn the numeric owner ID into a readable name with the POSIX extension when it is available.

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

$uid = fileowner($file);
chown($file, $uid);

if (function_exists("posix_getpwuid")) {
    $info = posix_getpwuid($uid);
    echo "Owner name: ", $info["name"], PHP_EOL;
} else {
    echo "Owner ID: ", $uid, PHP_EOL;
}

unlink($file);

How It Works

  1. We create a demo file and read its owner ID.
  2. chown() sets the same owner again.
  3. function_exists() checks if the POSIX extension is installed.
  4. posix_getpwuid() looks up the user name for the ID and we print it.

Frequently Asked Questions

It changes the owner of a file or folder. The owner can be given as a user name or as a numeric user ID.
Only root can give a file to another user. If PHP runs as a normal user, it can only set the owner to itself.
No. On Windows it does nothing and returns true, so the owner never changes.
chown() changes the owner user of the file, and chgrp() changes the owner group. Both are about ownership, while chmod() is about permissions.
Use fileowner($file) for the numeric user ID, and posix_getpwuid() to convert it to a user name.

Next: clearstatcache()

clearstatcache() clears the cache that PHP uses to remember file information. Functions su.

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