Friday, October 10, 2025
HomeLanguagesPHP chown( ) Function

PHP chown( ) Function

The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file.

Syntax:

bool chown ( $filename, $user )

Parameters: The chown() function in PHP accepts two parameters which are filename and user.

  1. $filename: It specifies the file whose owner you want to change.
  2. $user: It specifies the new owner. It can be a username or an user id.

Return Value: The chown() function returns true on success and false on failure.

Errors And Exception:

  1. The chown() function in PHP doesn’t works for remote files.It only works on files which are accessible by the server’s filesystem.
  2. PHP checks whether the files or directories which are being operated have the same owner as the script that is being executed or not when safe mode is enabled.

Examples:

Input : chown("gfg.txt", "shubrodeep")
Output : true

Input :  $path = "/user01/Desktop/neveropen/gfg.php";
         $user_name = "root";
         chown($path, $user_name); 
Output : true

Below programs illustrate the chown() function.

Program 1:




<?php
  
// Sets shubrodeep as owner
chown("gfg.txt", "shubrodeep");
  
?>


Output:

true

Program 2:




<?php
  
// Sets root as owner of the file "gfg.php"
$path = "/user01/Desktop/neveropen/gfg.php";
$user_name = "root";
chown($path, $user_name); 
  
?>


Output:

true

Reference:
http://php.net/manual/en/function.chown.php

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS