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

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

Example

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

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

Code execution result:

phptrue

Example

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

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

Code execution result:

phpfalse

Example

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

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

Code execution result:

phptrue

See Also

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