PHP Function Reference

PHP chown() Function



The PHP chown() function attempts to change the owner of the specified file. Only the superuser may change the owner of a file.

Note: This function doesn't work on remote files as the file to be examined must be accessible via the server's filesystem. On Windows, this function fails silently when applied on a regular file.

Syntax

chown(filename, user)

Parameters

filename Required. Specify the path to the file to change owner for.
user Required. Specify the new owner. Can be a user name or a user ID.

Return Value

Returns true on success or false on failure.

Example:

Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to change its owner.

<?php
$file = 'test.txt';

//changing the owner of the file
chown($file, "root");

//displaying the result
print_r(posix_getpwuid(fileowner($file)));
?>

The output of the above code will be:

Array
(
    [name] => root
    [passwd] => x
    [uid] => 0
    [gid] => 0
    [gecos] => root
    [dir] => /root
    [shell] => /bin/bash
)

❮ PHP Filesystem Reference