PHP Filesystem lchown() Function



The PHP Filesystem lchown() function is used to change the user ownership of a symbolic link. Only the superuser can change the ownership of the given file.

This is a link in the filesystem. And this function is not work on Windows platforms.

Syntax

Below is the syntax of the PHP Filesystem lchown() function −

bool lchown ( string $filename , mixed $user )

Parameters

Below are the required parameters of the lchown() function −

Sr.No Parameter & Description
1

$filename(Required)

It is the path to the symlink to change the owner.

2

$user(Required)

It is the new owner. So it can be a user name or a UID(user ID).

Return Value

The function returns TRUE on success or FALSE on failure.

PHP Version

The lchown() function was first introduced as part of core PHP 5.1.3 and work well with the PHP 7 and PHP 8.

Example

First we will see the basic usage of the PHP Filesystem lchown() function. So we will create a symbolic link and then, lchown() function will be used to change its owner.

<?php
    $target = "/PhpProject/PhpTest.php";
    $link  = "/PhpProject/test.html";
    if(symlink($target, $link)) {
        echo "The symbolic link is created.\n";
    } else {
        echo "The symbolic link is not created.\n";
    }

    //Change the group owner of the symbolic link
    if(lchown($link, 8)){
        echo "The ownership is successfully changed.\n";
    } else {
        echo "The ownership can not be changed.\n";
    }
?> 

Output

This will produce the following result −

The symbolic link is created.
The ownership is successfully changed.

Example

Here is another example to see how the lchown() function work to change the ownership of a symlink.

<?php
    $target = "/home/user/documents/report.pdf";
    $link = "/home/user/shortcut_report.pdf";
    if (symlink($target, $link)) {
        echo "The symbolic link is created.\n";
    } else {
        echo "The symbolic link is not created.\n";
    }

    // Change the group owner of the symbolic link
    if (lchown($link, 1001)) {
        echo "The ownership is successfully changed.\n";
    } else {
        echo "The ownership cannot be changed.\n";
    }
?>

Output

Here is the outcome of the following code −

The symbolic link is created.
The ownership is successfully changed.

Summary

The lchown() method is a built-in function to change the ownership of the a symbolic link. It returns true on success and false on failure.

php_function_reference.htm
Advertisements