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.
- target : It is a mandatory parameter which specifies the target.
- 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
- The link() function does not work on remote files as the file to be examined must be accessible via the server’s filesystem.
- The link created by the link() function is not an HTML link, but a link in the filesystem..
- In linux, hardlinking to a directory is not permitted.
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
php
<?php
$targetfile = 'gfg.txt';
$linkname = 'gfglink';
link($targetfile, $linkname);
?>
|
Output:
1
Program 2
php
<?php
$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!
Related Article: PHP | symlink( ) function
Reference:
http://php.net/manual/en/function.link.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Dec, 2021
Like Article
Save Article