PHP lchown() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, lchown() changes the owner of a symbolic link itself. It does not follow the link to the file it points to.

It is the link-safe partner of chown(). Use it when you want to change who owns a link, for example after you deploy a new release folder on a Linux server.

Syntax

Call lchown() with the values you need. Start with this line.

PHP
$ok = lchown('current', 'www-data');
CallWhat it does
lchown('current', 'www-data')Makes www-data the owner of the link named current.
lchown('current', 33)Does the same job by using the numeric user id.
lchown('missing-link', 'root')Returns false with a warning because the link does not exist.

lchown() is not available on Windows. On most systems only the superuser (root) can give a link to another user, so this function normally runs inside deployment scripts or command line tools, not inside a normal web request.

Library declarations
PHP
lchown(string $filename, string|int $user): bool

Parameters

ParameterDescription
filenameRequired. The path to the symbolic link.
userRequired. A user name as a string, or a user id as an integer.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Change the Owner of a Link

Create a symbolic link and assign it to another user. Run this as root.

PHP
<?php
$link = __DIR__ . '/current';
symlink('releases/v1', $link);

if (lchown($link, 'www-data')) {
    echo "Owner of the link changed", PHP_EOL;
} else {
    echo "Could not change the owner", PHP_EOL;
}

How It Works

  1. symlink() creates a link named current that points to releases/v1.
  2. lchown() asks the system to make www-data the owner of the link.
  3. Only the link is affected. The folder releases/v1 keeps its own owner.
  4. A true result prints the success message.

Example 2 — Check That the Function Exists

lchown() does not exist on Windows, so test for it before calling it.

PHP
<?php
$link = __DIR__ . '/current';

if (!function_exists('lchown')) {
    echo "lchown() is not available here", PHP_EOL;
} elseif (!@lchown($link, 'www-data')) {
    echo "Could not change the owner", PHP_EOL;
}

How It Works

  1. function_exists() tells you whether the operating system supports lchown().
  2. On Linux the function exists, so the script moves on to the next check.
  3. The call fails because the link is missing or the user has no permission.
  4. The @ hides the warning and the script prints a clear message.

Example 3 — Read the Owner with lstat()

Use lstat() to read the user id of the link itself after changing it.

PHP
<?php
$link = __DIR__ . '/current';
@unlink($link);
symlink('releases/v1', $link);

lchown($link, 33);

$info = lstat($link);
echo "Owner id: ", $info['uid'], PHP_EOL;

How It Works

  1. unlink() removes an old link so the script can run again.
  2. lchown() sets the owner of the link to the user id 33.
  3. lstat() reads data about the link itself and not the folder it points to.
  4. The uid value shows the new owner id.

Frequently Asked Questions

It changes the owner of a symbolic link without changing the owner of the file or folder the link points to.
chown() follows the link and changes the target. lchown() changes the link itself.
No. The function is not available on Windows. Use function_exists('lchown') to check first.
Most systems only allow root to give ownership to another user. Without the right privileges, PHP shows a warning and returns false.
Yes. You can pass either a user name string or an integer user id.

Next: link()

link() creates a hard link. A hard link is a second name for the same file on disk, so bot.

link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful