Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Filesystem Functions : link

link

Create a hard link (PHP 4, PHP 5)
bool link ( string target, string link )


Related Examples ( Source code ) » link
















Code Examples / Notes » link

kop

Note that link() will not work if the target already exists, at least as of php 4.1.2.

stephane

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);
}
?>


mmap

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.


guilherme garnier

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


jasper bekkers

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


root

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).

Change Language


Follow Navioo On Twitter
basename
chgrp
chmod
chown
clearstatcache
copy
delete
dirname
disk_free_space
disk_total_space
diskfreespace
fclose
feof
fflush
fgetc
fgetcsv
fgets
fgetss
file_exists
file_get_contents
file_put_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
flock
fnmatch
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
lchgrp
lchown
link
linkinfo
lstat
mkdir
move_uploaded_file
parse_ini_file
pathinfo
pclose
popen
readfile
readlink
realpath
rename
rewind
rmdir
set_file_buffer
stat
symlink
tempnam
tmpfile
touch
umask
unlink
eXTReMe Tracker