PHP | symlink( ) function

The symlink() function in PHP is an inbuilt function which is used to create a symbolic link for a target which already exists. It helps to create a specific name link for a target.
The target and link names are sent as parameters to the symlink() function and it returns True on success and False on failure.
The symlink() function does not provide an HTML link, but a link in the file system.

Syntax:

symlink(target, link)

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



  1. target : It is a mandatory parameter which specifies the target whose link has to be created.
  2. link : It is a mandatory parameter which specifies the link name.

Return Value:
It returns True on success and False on failure.

Errors And Exception

  1. The symlink() function does not work if the system you run PHP is prior to Windows Vista/Windows Server 2008.
  2. The symlink() function accepts only absolute paths on windows. Relative paths on windows are not supported for symlinks.
  3. The symlink() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.

Examples:

Input : $target_pointer = 'gfg.txt';
        $link_name = 'geeksforgeeks';
        symlink($target_pointer, $link_name);
Output : 1

Input : $target_pointer = "/home/user1/gfg.txt";
        $link_name = 'mylink';
        $test = symlink($target_pointer, $link_name);
        if ($result) 
        {
          echo ("Symlink has been created!");
        }
        else 
        {
          echo ("Symlink cannot be created!");
        }
Output : Symlink has been created!

Below programs illustrate the touch() function.

Suppose there is a file named “gfg.txt”

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// specifying target
$target_pointer = 'gfg.txt';
  
// specifiying link  name
$link_name = 'geeksforgeeks';
  
// creating alink using symlink() function
symlink($target_pointer, $link_name);
?>

chevron_right


Output:

1

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// specifying target
$target_pointer = "/home/user1/gfg.txt";
  
// specifiying link  name
$link_name = 'mylink';
  
// creating alink using symlink() function
$test = symlink($target_pointer, $link_name);
if ($result
{
   echo ("Symlink has been created!");
}
else 
{
   echo ("Symlink cannot be created!");
}
  
?>

chevron_right


Output:

Symlink has been created!

Reference:
http://php.net/manual/en/function.symlink.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.