PHP Filesystem chown() Function
The PHP Filesystem chown() function is used to to change the owner of a specified file. And with this function only the superuser can change the owner of a file. It returns true on success, otherwise false on failure.
Syntax
Below is the syntax of the PHP Filesystem chown() function −
bool chown ( string filename, string/int user )
Parameters
The parameters are needed to use the chown() function are mentioned below −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
filename(Required) The file path whose owner need to be changed. |
| 2 |
user(Required) The new owner of the file. It can be user name or user id or number. |
Return Value
It returns TRUE on success, otherwise FALSE on failure.
PHP Version
The chown() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
The PHP Filesystem chown() function is used in the below code to change the owner of myfile.txt to the user root and then prints details about the new owner of the file.
Also we have used stat() and posix_getpwuid() functions. The stat() gets information about a file, while posix_getpwuid() gets user information by their user ID.
<?php
// File name and username to use
$filename= "myfile.txt";
$dirpath = "/Applications/XAMPP/xamppfiles/htdocs/mac/" . $filename ;
$user_name = "root";
// Set the user
chown($dirpath, $user_name);
// Check the result
$stat = stat($dirpath);
print_r(posix_getpwuid($stat['uid']));
?>
Output
This will produce the following result −
Array ( [name] => root [passwd] => ******** [uid] => 501 [gid] => 20 [gecos] => root [dir] => /root [shell] => /bin/zsh )
Example
In this case, we will try to change the owner of a file named "myfile.txt" located in a user's home directory to a user named "nick". This can be useful when changing access permissions or need many users to control the same file inside a system.
<?php // Define the file name and path $filename = "myfile.txt"; $dirpath = "/home/nick/documents/" . $filename; // Define the user name $user_name = "nick"; // Change the owner of the file to "nick" chown($dirpath, $user_name); echo "Changed Owner for User's Home Directory is: ". $user_name; ?>
Output
This will generate the below result −
Changed Owner for User's Home Directory is: nick
Example
This PHP code will change the owner of a file or directory to "www-data" for a web server by transferring ownership to the user account that the web server software uses to access and serve web content.
<?php // declare the file name and path $filename = "index.html"; $dirpath = "/var/www/html/" . $filename; // declare the user name $user_name = "www-data"; // change the owner of the file to "www-data" chown($dirpath, $user_name); // check the result $stat = stat($dirpath); print_r(posix_getpwuid($stat['uid'])); ?>
Output
This will create the below outcome −
Array ( [name] => root [passwd] => * [uid] => 0 [gid] => 0 [gecos] => System Administrator [dir] => /var/root [shell] => /bin/sh )
Example
In this can, we will change the owner of the file "upload.txt" in the FTP directory to the user named ftpuser. This user handles files using FTP.
<?php $filename = "upload.txt"; $dirpath = "/home/ftpuser/ftp/" . $filename; // Define the user name $user_name = "ftpuser"; // Change the owner of the file to "ftpuser" chown($dirpath, $user_name); // Check the result $stat = stat($dirpath); print_r(posix_getpwuid($stat['uid'])); ?>
Output
This will create the following outcome −
Array ( [name] => root [passwd] => * [uid] => 0 [gid] => 0 [gecos] => System Administrator [dir] => /var/root [shell] => /bin/sh )
Note
- On Windows platforms, this feature is not available.
- This function cannot work on remote files as it is unable to access the filesystem.
Summary
The PHP filesystem function chown() can be used to change a file's owner. By giving proper ownership, it allows you to handle your directories and files easier.