Thursday, October 16, 2025
HomeLanguagesPHP | symlink( ) function

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 = 'neveropen';
        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 = 'neveropen';
 
// 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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS