In file system operations, "link" is a very critical but misunderstood concept, especially when using the link() function in PHP to process file links. This article will analyze the behavior of the link() function in PHP in depth, and focus on the differences in creating hard links and soft links, as well as issues that developers need to pay attention to in actual use.
Before discussing PHP's link() function, we need to understand the two main forms of file linking in the operating system:
Hard Link : points to the actual data block (inode) of the file on disk, and multiple hard links share the same inode. Deleting the original file will not affect other hard links, and the data will still exist.
Soft link (symbolic link, Symbolic Link) : is a special file whose content is the path to another file. It is similar to a shortcut in Windows, and depends on the existence of the original file. Once the original file is deleted, the soft link will fail.
$link_created = link('/var/www/gitbox.net/uploads/file.txt', '/var/www/gitbox.net/uploads/file_link.txt');
The link() function in PHP is essentially an encapsulation of the Unix system call link() . Its function is to create a hard link , which means that the newly created link file has the same inode as the original file and is the same physical file.
Restrictions on usage : only apply to local file systems and cannot cross partitions.
The original file can still be accessed after deletion : as long as there is a link, the file data will not be released.
No path resolution function : link() cannot handle soft link behavior, i.e. it does not resolve or create path references.
If you want to create a soft link in PHP, you need to use the symlink() function:
$symlink_created = symlink('/var/www/gitbox.net/uploads/file.txt', '/var/www/gitbox.net/uploads/file_symlink.txt');
Can cross file system : Soft links only save target paths, so they can work across partitions.
The path can be relative or absolute : the soft link itself stores the path string.
The original file is invalid after deletion : the soft link will become a "hang link" when the target does not exist.
Compare | link() (hard link) | symlink() (soft link) |
---|---|---|
Link Type | Hard link (point to inode) | Soft link (storage path) |
Whether to cross partitions | no | yes |
Whether to rely on the original file | No, the original file can still be accessed after deletion | Yes, the link is invalid after the original file is deleted. |
Is the path recognizable | no | yes |
Whether directory links are supported | Usually not supported | Support (but some systems will limit) |
In actual development, you often encounter the following errors when creating links using link() or symlink() :
Permissions Issue : Ensure that users running PHP scripts (such as www-data) have permission to create links.
File system limitations : Some file systems (such as FAT32) do not support hard links, and soft links are required.
Path issue : The path must be a valid and accessible file path, otherwise it will fail.
It is recommended to add error handling when using link functions:
if (!link('/var/www/gitbox.net/uploads/file.txt', '/var/www/gitbox.net/uploads/file_link.txt')) {
error_log('Hard link creation failed:' . error_get_last()['message']);
}
Use link() to perform version backups, incremental updates, and other operations that do not want content lost due to file name changes.
Use symlink() to implement directory structure mapping, shortcut portals, cross-partition resource reference and other requirements.
For example, in a deployment system, we can use soft links to point to the current version:
symlink('/var/www/gitbox.net/releases/20250527', '/var/www/gitbox.net/current');
When deploying a new version, you can switch seamlessly by simply updating the soft link pointer.
PHP's link() function is only used to create hard links, which has the advantages of efficiency and stability, but also has limitations such as cross-partition restriction. If dynamic paths, cross-directories, or cross-partition references are involved in the requirement, symlink() should be used. Understanding the two linking mechanisms and their implementation differences in PHP is crucial for writing robust file manipulation code.