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');
Call
What 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
Parameter
Description
target
Required. The path of the existing file.
link
Required. The path of the new hard link that will be created.
Return Value
Return Value
Description
bool
Returns 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.