Current Location: Home> Latest Articles> PHP lchown() Function Explained: How to Change File and Directory Owners

PHP lchown() Function Explained: How to Change File and Directory Owners

gitbox 2025-06-18

1. Introduction to the lchown() Function

The lchown() function is a file permission modification function in PHP, used to change the owner of a symbolic link’s file or directory. It is similar to the chown command in Linux, but it only affects the last symbolic link in the path. If there are multiple symbolic links in the path, the chown() function should be used instead.

2. Syntax and Parameters of the lchown() Function

The syntax of the lchown() function is as follows:


bool lchown(string $filename, mixed $user)

The lchown() function has two parameters:

  • filename: The file or directory path whose permissions need to be modified.
  • user: The username or user ID of the target owner.

3. Usage Examples of the lchown() Function

3.1 Changing File Owner

Here’s an example showing how to use the lchown() function to change the file owner:


$filename = "/var/www/html/test.txt";
$user = "root";
if (lchown($filename, $user)) {
    echo "File owner changed to: " . $user;
} else {
    echo "Failed to change file owner";
}

The code above changes the owner of the test.txt file to the root user. If successful, it will print “File owner changed to: root”; otherwise, it will print “Failed to change file owner”.

3.2 Changing Directory Owner

Here’s an example showing how to use the lchown() function to change the owner of a directory:


$dirname = "/var/www/html/test_dir";
$user = "root";
if (lchown($dirname, $user)) {
    echo "Directory owner changed to: " . $user;
} else {
    echo "Failed to change directory owner";
}

The code above changes the owner of the test_dir directory to the root user. If successful, it will print “Directory owner changed to: root”; otherwise, it will print “Failed to change directory owner”.

3.3 Changing File or Directory Owner to Numeric UID/GID

You can also change the owner of a file or directory to a numeric user ID (UID) or group ID (GID). Here’s an example:


$filename = "/var/www/html/test.txt";
$user_id = 1001;
if (lchown($filename, $user_id)) {
    echo "File owner changed to: " . $user_id;
} else {
    echo "Failed to change file owner";
}

The code above changes the owner of the test.txt file to the user with UID 1001. If successful, it will print “File owner changed to: 1001”; otherwise, it will print “Failed to change file owner”.

4. Precautions When Using the lchown() Function

Here are a few important things to keep in mind when using the lchown() function:

  • Only superusers (root) can change the owner of a file or directory.
  • If you try to change the owner of a file or directory that does not belong to the current user, a warning level error will be thrown.
  • The lchown() function only modifies the owner of the last symbolic link in the path. If there are multiple symbolic links, you need to use the chown() function instead.

5. Conclusion

The lchown() function is used in PHP to change the owner of a file or directory. It only affects the last symbolic link in the path. If there are multiple symbolic links, you need to use the chown() function. Make sure you have superuser privileges when using this function.