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.
link
(PHP 4, PHP 5)
link — Crea un enlace duro
Descripción
bool link
( string $target
, string $link
)
link() crea un enlace duro.
Parámetros
- target
-
El objetivo del enlace.
- link
-
El nombre del enlace.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Notas
Note: Sólo para Windows: Esta función requiere que PHP se ejecute en un modo elevado o con UAC deshabilitado.
Historial de cambios
| Versión | Descripción |
|---|---|
| 5.3.0 | Esta función ahora está disponible en plataformas Windows (Vista, Server 2008 o mayores). |
Ejemplos
Example #1 Crear un simple enlace duro
<?php
$objetivo = 'origen.ext'; // Este es el archivo que ya existe
$enlace = 'nuevo_archvo.ext'; // Este es el nombre de archivo que quiere enlazar
link($objetivo, $enlace);
?>
Notas
Note: Esta función no funcionará en ficheros remotos ya que el fichero debe ser accesible vía el sistema de ficheros del servidor para poder ser examinado.
Ver también
- symlink() - Crea un enlace simbólico
- readlink() - Devuelve el objetivo de un enlace simbólico
- linkinfo() - Obtiene información acerca de un enlace
Anonymous
16-Oct-2009 08:54
albertpeschar at gmail dot com
28-May-2008 03:20
>> 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
28-Mar-2008 01:04
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
06-Mar-2008 05:08
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
10-Jan-2008 02:56
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
06-Aug-2007 06:45
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
05-Jan-2007 09:58
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
02-Aug-2006 02:16
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
25-Apr-2006 06:32
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
15-May-2004 11:28
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
25-Sep-2003 09:20
Note that link() will not work if the target already exists, at least as of php 4.1.2.
