(PHP 4, PHP 5)
link — 建立一个硬连接
$target
, string $link
)link() 建立一个硬连接。
target
要链接的目标。
link
链接的名称。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Note: Windows下:该功能需要以 elevated 模式运行,或者关闭 UAC。
版本 | 说明 |
---|---|
5.3.0 | 该功能在 Windows 平台下开始有效(Vista、 Server 2008 或更高版本)。 |
Example #1 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: 此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。
jurgens at prefix dot co dot za (2012-07-11 10:25:05)
For the die hard eZ Components fans out there, the fact that the link function is supported from PHP 5.3 on certain Windows platforms breaks the ezcArchiveZip class.
It's quite specific, but if you're running PHP 5.3 on a windows platform that supports link, and you're trying to add more than one file to your archive, it will think that all subsequent files are symbolic links to the first file.
This happens because the ArchiveZip class assumes that if the link function is supported, inodes (see the documentation on the stat function - http://php.net/manual/en/function.stat.php) are supported as well.
Unsigned (2011-10-05 22:25:05)
link() actually does work on Windows XP SP3 running PHP 5.3.8 on an NTFS filesystem, even though the documentation says Vista and later.
The corresponding symlink() does not however.
Anonymous (2009-10-16 01:54:56)
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.
albertpeschar at gmail dot com (2008-05-28 08:20:16)
>> Be aware that the filesystem of the target and the link must be the same,
>> otherwise the link will fail! (Linking files over different filesystems doesn't work under Unix).
Using debian sid, 2.6.24-17-generic, that is definitely not true.
Tim McCormack (2008-03-27 17:04:38)
As others have noted, the parameter order can seem a little awkward. Here's a mnemonic for remembering the order:
In UNIX, commands go like this:
> command input output
The link command is no different:
> ln infile outfile
...and PHP respects that convention.
Jasper Bekkers (2008-03-06 09:08:51)
Due to the acquisition of Winternals by Microsoft, the previous link to junction.exe doesn't work anymore. The file has been moved to http://www.microsoft.com/technet/
sysinternals/FileAndDisk/Junction.mspx
(without the newline)
me at robhaswell dot co dot uk (2008-01-09 18:56:39)
For those that find the order of the arguments unclear, here is a more verbose description:
<?php
$source = "something.ext"; // This is the file that already exists
$dest = "newfile.ext"; // This the filename that you want to link it to
link($source, $dest);
?>
stephane AT baladeauboutdumonde DOT com (2007-08-06 11:45:07)
Make link recursively :
<?php
function makeRecusLink($orig, $dest)
{
if (is_dir($orig)) {
if (substr($orig, -1) != '/') {
$orig .= '/';
}
$handle = opendir($orig);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path = $orig.$file;
if (is_file($path)) {
@link($path, $dest.'/'.$file);
} else if (is_dir($path)) {
@mkdir($dest.'/'.$file, 0755);
makeRecusLink($path, $dest.'/'.$file);
}
}
}
}
closedir($handle);
}
?>
root at c-works dot net (2007-01-05 01:58:28)
Be aware that the filesystem of the target and the link must be the same, otherwise the link will fail! (Linking files over different filesystems doesn't work under Unix).
Jasper Bekkers (2006-08-02 07:16:51)
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
Guilherme Garnier (2006-04-25 11:32:10)
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
mmap at upt dot org (2004-05-15 04:28:30)
I think kop is confused regarding the semantics of link's argument order. The user's comment states that target should not already exist, suggesting that it is the target that is being created. As with the UNIX hardlink, ln(1), the target is the existing file. I think kop meant to say php's link() will return an error if the second parameter, the link being created, already exists.
Also, as with the UNIX system call link will fail if the link being created exists on a different filesystem.
kop at meme dot com (2003-09-25 02:20:49)
Note that link() will not work if the target already exists, at least as of php 4.1.2.