Current Location: Home> Latest Articles> PHP link() Function Explained: How to Create Hard Links and What to Watch Out For

PHP link() Function Explained: How to Create Hard Links and What to Watch Out For

M66 2025-06-15

1. What is the link() Function

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

To understand hard links, it's essential to first understand inodes. An inode is a data structure in a file system that stores metadata about a file. When the operating system creates a file, it generates an inode that holds key information about the file, such as its name, permissions, size, and owner. Hard links work by having different filenames point to the same inode.

In contrast, symbolic links (soft links) are special files that contain a path to another file. They can span across file systems and can even point to non-existent files.

2. Syntax of the link() Function

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

  
bool link(string $target, string $link)  

Parameter description:

  • target: The existing file you want to create a hard link to. This file must already exist.
  • link: The name of the link to be created.

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

3. Return Value of link() Function

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

4. Example of link() Function

Here is an example of creating 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.";  
}  

This code creates a hard link pointing to the file /var/www/html/test.txt and names the new link /var/www/html/link_test.txt. If the operation is successful, the system outputs "Link created successfully!".

5. Precautions When Using link() Function

  • Hard links can only be created within the same file system and are not supported across different file systems.
  • When creating a hard link, the target and the link share the same inode number, which means they also share attributes such as ownership, permissions, and file size.
  • Hard links do not consume additional disk space, as they merely assign a new filename to an existing file.
  • A file is only truly deleted when all of its hard links are removed.
  • Only superusers can create hard links across different file systems, but even then, it is typically not permitted.

6. Use Cases for the link() Function

Although the link() function isn't commonly used in day-to-day programming, it serves specific purposes:

  • It can improve access speed for frequently read configuration files by creating hard links in strategic locations.
  • For files that need to be stored in multiple locations, using hard links can reduce storage costs by keeping them tied to the same inode.
  • In certain scenarios, such as creating backups or filesystem mirrors, hard links can be particularly useful.

7. Conclusion

The link() function in PHP is a file system function used to create hard links. Hard links work by associating a new filename with an existing file, allowing multiple filenames to point to the same inode. Depending on the context, hard links can help reduce storage requirements and speed up file access. However, when creating hard links, it's important to ensure that both files reside in the same file system, and note that creating hard links across file systems requires superuser privileges.