PHP 檔案系統 lchown() 函式



PHP 檔案系統 lchown() 函式用於更改符號連結的使用者所有權。只有超級使用者才能更改給定檔案的所有權。

這是一個檔案系統中的連結。此函式在 Windows 平臺上不起作用。

語法

以下是 PHP 檔案系統 lchown() 函式的語法 -

bool lchown ( string $filename , mixed $user )

引數

以下是 lchown() 函式所需的的引數 -

序號 引數及描述
1

$filename(必需)

它是要更改所有者的符號連結的路徑。

2

$user(必需)

它是新的所有者。所以它可以是使用者名稱或 UID(使用者 ID)。

返回值

該函式在成功時返回 TRUE,失敗時返回 FALSE。

PHP 版本

lchown() 函式最初作為 PHP 5.1.3 的核心部分引入,並且與 PHP 7 和 PHP 8 相容良好。

示例

首先,我們將看到 PHP 檔案系統 lchown() 函式的基本用法。因此,我們將建立一個符號連結,然後使用 lchown() 函式更改其所有者。

<?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";
    }
?> 

輸出

這將產生以下結果 -

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

示例

這是另一個示例,用於瞭解 lchown() 函式如何工作以更改符號連結的所有權。

<?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";
    }
?>

輸出

以下是以下程式碼的結果 -

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

總結

lchown() 方法是更改符號連結所有權的內建函式。它在成功時返回 true,失敗時返回 false。

php_function_reference.htm
廣告