PHP chown() Function: Changing File Ownership
The article explains PHP's chown() function, detailing its purpose, syntax, parameters, return values, and provides a complete example with code and expected output for changing a file's owner on a server.
The PHP chown() function changes the owner of a specified file to a given user, which can be identified by name or UID; only the superuser can perform this operation.
Signature: bool chown(string $filename, mixed $user) Parameters:
filename : Path to the file.
user : Username or numeric ID of the new owner.
Return value: TRUE on success, FALSE on failure.
Example usage:
<?php
// File name and username to use
$file_name = "foo.php";
$path = "/home/sites/php.net/public_html/sandbox/" . $file_name;
$user_name = "root";
// Set the user
chown($path, $user_name);
// Check the result
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));
?>Sample output of the above script might be:
Array
[
[name] => root
[passwd] => x
[uid] => 0
[gid] => 0
[gecos] => root
[dir] => /root
[shell] => /bin/bash
]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
