Open In App

PHP | symlink( ) function

Improve
Improve
Like Article
Like
Save
Share
Report

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 symlink() function.
Suppose there is a file named ‘gfg.txt’
Program 1

php




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


Output:  

1

Program 2

php




<?php
// specifying target
$target_pointer = "/home/user1/gfg.txt";
 
// specifying 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!");
}
 
?>


Output:  

Symlink has been created!

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



Last Updated : 30 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads