link()
(PHP 4, PHP 5, PHP 7)
建立一个硬连接
说明
link(string $target,string $link): bool
link()建立一个硬连接。
参数
- $target
要链接的目标。
- $link
链接的名称。
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
注释
Note:Windows下:该功能需要以 elevated 模式运行,或者关闭 UAC。
更新日志
版本 | 说明 |
---|---|
5.3.0 | 该功能在 Windows 平台下开始有效(Vista、 Server 2008 或更高版本)。 |
范例
Creating a simple hard link
<?php $target = 'source.ext'; // This is the file that already exists $link = 'newfile.ext'; // This the filename that you want to link it to link($target, $link); ?>
注释
Note:此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。
参见
symlink()
建立符号连接readlink()
返回符号连接指向的目标linkinfo()
获取一个连接的信息
to clarify: in unix/linux: hardlinks (by this function) cannot go across different filesystems. softlinks can point anywhere. in linux, hardlinking to directory is not permited.
For a backup utility I needed link-like functionality on a windows system. As it isn't availible on windows, i tried to do it myself with the help of some tools. All you need is junction.exe from sysinternals in your %PATH%. <?php if(!function_exists('link')){ // Assume a windows system function link($target, $link){ if(is_dir($target)){ // junctions link to directories in windows exec("junction $link $target", $lines, $val); return 0 == $val; }elseif(is_file($target)){ // Hardlinks link to files in windows exec("fsutil hardlink create $link $target", $lines, $val); return 0 == $val; } return false; } } ?> http://www.sysinternals.com/Utilities/Junction.html
In at least php-5.3 (linux-2.6.38.6) a process owned by apache could make a link() in a directory owned by apache, to a file owned by webmaster to which it had group read permissions. In php-7.0 (linux-4.13.16) that results in a "permission denied". Either the target file must be owned by apache or one must use copy() instead (so that ownership changes to apache).
I noticed that, differently from Unix ln command, the second parameter can´t be a directory name, i.e., if you want to create a link with the same filename of the target file (obviously on different directories), you must specify the filename on the link parameter. Example: Unix ln command: ln /dir1/file /dir2/ // ok, creates /dir2/file link PHP link function: link ("/dir1/file", "/dir2/"); // wrong, gives a "File exists" warning link ("/dir1/file", "/dir2/file"); // ok, creates /dir2/file link