(PHP 4, PHP 5, PHP 7, PHP 8)
link — ハードリンクを作成する
targetリンクの対象。
linkリンクの名前。
link が既に存在していたり、
target が存在しない場合には、
この関数は失敗し E_WARNING が発生します。
例1 シンプルなハードリンクの作成
<?php
$target = 'source.ext'; // これは、既存のファイル名です
$link = 'newfile.ext'; // これは、リンク先としたいファイル名です
link($target, $link);
?>注意: この関数では、 リモートファイル を 使用することはできません。これは、処理されるファイルがサーバーの ファイルシステムによりアクセスできる必要があるためです。
注意: Windows のみ: この関数を使用するには、 権限を昇格させたモード、あるいは UAC を無効にした状態で PHP を実行しなければなりません。
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 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.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