Current Location: Home> Latest Articles> PHP link() Function Explained: How to Create Hard Links and Key Considerations

PHP link() Function Explained: How to Create Hard Links and Key Considerations

M66 2025-06-13

1. What is the link() Function

The link() function is a file system function in PHP used to create hard links. A hard link connects a new filename to an existing file within the same file system, effectively allowing both filenames to reference the same file. By creating a hard link, multiple filenames can point to the same inode, enabling access to the same file from different locations.

To understand hard links, you first need to understand inodes (index nodes). An inode is a data structure in a file system that stores metadata about a file. When the operating system creates a file, it assigns an inode to hold information such as the file name, permissions, size, and owner. A hard link works by pointing multiple filenames to the same inode.

In contrast, a soft link (symbolic link) is a special file that contains the path to another file. Soft links can span across file systems and can point to files that do not exist.

2. Syntax of the link() Function

The syntax of the link() function is as follows:


bool link(string $target, string $link)

Explanation of parameters:

  • target: The target file for the hard link. This must be an existing file.
  • link: The name of the new hard link to be created.

Note that both files must reside within the same file system.

3. Return Value of link()

The link() function returns true on success and false on failure.

4. Example of link()

Here’s an example demonstrating how to create a hard link:


$target = "/var/www/html/test.txt";
$link = "/var/www/html/link_test.txt";
if (link($target, $link)) {
    echo "Link created successfully!";
} else {
    echo "Failed to create link!";
}

The code above creates a hard link to the file /var/www/html/test.txt and names the new link /var/www/html/link_test.txt. If successful, it will output “Link created successfully!”.

5. Important Notes on link()

  • Hard links can only be created within the same file system; cross-file system hard links are not supported.
  • When a hard link is created, the target file and the new link share the same inode number, so they have the same owner, permissions, and file size.
  • Hard links do not consume additional disk space because they are just additional filenames pointing to the same file.
  • A file is only truly deleted when all hard links to it have been removed.
  • Only superusers can create hard links across different file systems.

6. Use Cases for link()

Although the link() function is not commonly used in everyday programming, it has several niche use cases:

  • Hard links can be used to improve access speed to frequently read configuration files.
  • Files that require multiple copies can be stored as hard links to reduce storage costs by pointing to the same inode.
  • In certain scenarios like file system backups or mirroring, hard links can be highly beneficial.

7. Conclusion

The link() function in PHP is used to create hard links, which allow multiple filenames to point to the same inode by linking to an existing file. Hard links can help reduce storage usage and enhance file access performance in some scenarios. However, it's important to remember that both the target and the new link must reside on the same file system, and superuser privileges are required to create links across file systems.