PHP link() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, link() creates a hard link. A hard link is a second name for the same file on disk, so both names open the same data.

This is different from a symbolic link made with symlink(). A hard link keeps working even if you delete the original name, because the data stays until the last name is removed.

Syntax

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

PHP
$ok = link('original.txt', 'copy.txt');
CallWhat it does
link('original.txt', 'copy.txt')Creates copy.txt as a second name for original.txt.
link(__DIR__ . '/a.log', __DIR__ . '/b.log')Uses full paths to create the link in a known folder.
link('missing.txt', 'alias.txt')Returns false with a warning because the target does not exist.

Hard links only work inside one disk or partition, and the target must be a file, not a folder. The new link name must not exist yet. Changes to the content show through both names, because there is only one copy of the data.

Library declarations
PHP
link(string $target, string $link): bool

Parameters

ParameterDescription
targetRequired. The path of the existing file.
linkRequired. The path of the new hard link that will be created.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Create a Hard Link

Make a second name for a file and read the same text through it.

PHP
<?php
$original = __DIR__ . '/original.txt';
$copy     = __DIR__ . '/copy.txt';

file_put_contents($original, 'Hello');

if (link($original, $copy)) {
    echo file_get_contents($copy), PHP_EOL;
}

unlink($copy);

How It Works

  1. file_put_contents() creates original. txt with the text Hello.
  2. link() gives the same data a second name, copy. txt.
  3. Reading copy. txt returns the text that was saved in original.txt.
  4. unlink() removes the extra name at the end so the script can run again.

Example 2 — Link to a File That Does Not Exist

link() returns false when the target is missing, so check the result.

PHP
<?php
$target = __DIR__ . '/nofile.txt';
$alias  = __DIR__ . '/alias.txt';

if (@link($target, $alias)) {
    echo "Link created", PHP_EOL;
} else {
    echo "Could not create the link", PHP_EOL;
}

How It Works

  1. nofile. txt does not exist, so there is nothing to link to.
  2. link() fails and returns false.
  3. The @ hides the PHP warning so the script can show its own message.
  4. The same failure happens if alias. txt already exists.

Example 3 — Count the Names of a File

After creating a hard link, stat() shows that the file now has two names.

PHP
<?php
$original = __DIR__ . '/data.txt';
$copy     = __DIR__ . '/data-link.txt';

file_put_contents($original, 'CodeToFun');
link($original, $copy);

$info = stat($original);
echo "Links: ", $info['nlink'], PHP_EOL;

unlink($copy);

How It Works

  1. A new file starts with one link, which is its own name.
  2. link() adds a second name, so the link count becomes 2.
  3. stat() returns an array, and the nlink key holds that count.
  4. Removing the extra name with unlink() lowers the count back to 1.

Frequently Asked Questions

It creates a hard link, which is another name for an existing file. Both names point to the same data on disk.
link() makes a hard link that shares the data. symlink() makes a shortcut that only stores the path of the target.
copy() makes a new independent file. link() makes no new data, so a change to one name appears in the other.
No. Hard links to folders are not allowed. Use symlink() if you need a link to a folder.
The target may be missing, the new name may already exist, the files may be on different disks, or the user may not have permission.

Next: linkinfo()

linkinfo() checks that a link exists and returns the device number of the disk that holds .

linkinfo() 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