276 of 410 menu

The chown Function

The chown function changes the owner of the specified file or directory. It accepts the file path as the first parameter, and the username or its ID as the second. Returns true on success and false on error.

Syntax

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

Example

Change the file owner to user 'www-data':

<?php $res = chown('example.txt', 'www-data'); var_dump($res); ?>

Code execution result:

true

Example

Try to change the owner of a non-existent file:

<?php $res = chown('nonexistent.txt', 'www-data'); var_dump($res); ?>

Code execution result:

false

Example

Change the owner by user ID (for example, 1000):

<?php $res = chown('example.txt', 1000); var_dump($res); ?>

Code execution result:

true

See Also

  • the chmod function,
    which changes access permissions
  • the chgrp function,
    which changes the file group
byenru