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
$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
- file_put_contents() creates the demo file.
- fileowner() returns the numeric ID of the current owner.
- chown() sets the owner to that same ID, which a normal user may do.
- unlink() deletes the demo file at the end.

