Current Location: Home> Latest Articles> Detailed explanation of the basic usage of PHP lchown function

Detailed explanation of the basic usage of PHP lchown function

gitbox 2025-05-29

When using PHP to handle file system operations, control of permissions and ownership is one of the tasks that developers often encounter. PHP provides a series of functions to manipulate the owner information of a file or directory, where lchown() is a function used to modify the owner of a symbolic link. This article will quickly learn about the basic syntax , usage methods and some things to note about.

1. Introduction to lchown() function

lchown() is a function in PHP used to change the owner of a symbolic link file. Unlike chown() , lchown() does not follow the symbol to link to the target file, but acts directly on the link itself.

This is very useful in some scenarios, especially when you want to operate links instead of actual files, such as doing deployment scripts or processing automatically generated link files.

2. Basic grammar

 bool lchown ( string $filename , string|int $user )
  • $filename : The path to the owner's symbolic link.

  • $user : New owner. It can be a user name string or a user's UID.

The return value is Boolean:

  • Return true successfully;

  • Failed to return false .

3. Use examples

Suppose we have a symbolic link /var/www/html/link_to_config , we want to change its owner to user deploy .

 <?php
$link = '/var/www/html/link_to_config';
$user = 'deploy';

if (lchown($link, $user)) {
    echo "The symbolic link owner has successfully modified it";
} else {
    echo "Modification failed,Please check if the permissions or paths are correct";
}
?>

Note: lchown() may require the script to run with superuser (such as root) permissions in order to successfully change the file owner.

4. Common application scenarios

  • Automatically set link ownership when deploying the system : For example, in an automated deployment script, ensure that symbolic links are owned by specific users.

  • Permission management policy : Set an exclusive owner for linked files to distinguish between ordinary files.

  • Multi-user system : Ensure that link permissions are clearly allocated in the file system structure.

5. Important things to note

  1. Operating system compatibility : lchown() is only valid on Unix-like systems (such as Linux, macOS). Windows does not support it.

  2. Permission Restrictions : Changing file owners usually requires administrator permissions, otherwise it will fail.

  3. Function behavior limitation : Functions do not operate the target file with symbolic links, but only act on the link itself.

6. Verification effect

You can use the command line tool to see if the link's owner has been changed:

 ls -l /var/www/html | grep link_to_config

Or use PHP scripts:

 <?php
echo fileowner('/var/www/html/link_to_config');
?>

If you need to get the username, you can use it in combination with posix_getpwuid() :

 <?php
$uid = fileowner('/var/www/html/link_to_config');
$userinfo = posix_getpwuid($uid);
echo $userinfo['name'];
?>

7. Alternative Methods

If what you need to modify is the owner of the target file that the link points to, rather than the link itself, use the chown() function:

 chown('/var/www/html/link_to_config', 'deploy');

This call will modify the actual file owner to which the link points, not the link itself.