chown()
chown() — Changes file owner.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.
Signature
chown(string $filename, string|int $user): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | Path to the file. |
| $user | string|int | A user name or number. |
Return value
Returns true on success or false on failure.
Example
A runnable snippet:
// File name and username to use $file_name= "foo.php"; $path = "/home/sites/php.net/public_html/sandbox/" . $file_name ; $user_name = "root"; // Set the user chown($path, $user_name); // Check the result $stat = stat($path); print_r(posix_getpwuid($stat['uid']));
Output:
Array ( [name] => root [passwd] => x [uid] => 0 [gid] => 0 [gecos] => root [dir] => /root [shell] => /bin/bash )
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page chown() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.