PHP | link( ) Function

The link() creates a hard link for a specified target. The target and the link are passed as parameters to the link() function and it returns true on success and false on failure.

Syntax:

link(target, link)

Parameters Used:
The link() function in PHP accepts two parameters.

  1. target : It is a mandatory parameter which specifies the target.
  2. link : It is an mandatory parameter which specifies the name of the link.

Return Value:
It returns TRUE on success or FALSE on failure.

Errors And Exception

  1. The link() function does not work on remote files as the file to be examined must be accessible via the server’s filesystem.
  2. The link created by the link() function is not an HTML link, but a link in the filesystem..
  3. In linux, hardlinking to a directory is not permited.

Examples:

Input : $targetfile = 'gfg.txt.'; 
        $linkname = 'gfglink';
        link($targetfile, $linkname);
Output : 1

Input : $targetfile = 'gfg.txt.'; 
        $linkname = 'gfglink';

        if(!link($targetfile, $linkname))
        {
           echo('Link has been created!');
        }
        else
        {
          echo('Link cannot be created!');
        }
Output : Link has been created!

Below programs illustrate the link() function.

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// target file
$targetfile = 'gfg.txt'
  
// name of the link
$linkname = 'gfglink';
  
// creating a symbolic link for the target file
link($targetfile, $linkname);
?>

chevron_right


Output:

1

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// target file
$targetfile = 'gfg.txt'
  
// name of the link
$linkname = 'gfglink';
  
// creating a symbolic link for the target file
if(!link($targetfile, $linkname))
 
    echo('Link has been created!');
 }
else
 {
    echo('Link cannot be created!');
 }
?>

chevron_right


Output:

Link has been created!

Related Article: PHP | symlink( ) function

Reference:
http://php.net/manual/en/function.link.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.