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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP chown() Function: Changing File Ownership

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
]
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendFilesystemchownfile-permissions
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.