PHP symlink() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, symlink() creates a symbolic link, which is a shortcut that points to another file or folder.

Reading from the link gives you the content of the target. The link itself is only a small pointer, so it uses almost no disk space and does not copy the data.

Syntax

Call symlink() with the values you need. Start with this line.

PHP
$ok = symlink('/var/www/shared/logo.png', 'logo.png');
CallWhat it does
symlink('/data/report.txt', 'report-link.txt')Creates report-link.txt that points to /data/report.txt.
symlink(__DIR__ . '/storage', __DIR__ . '/public/storage')Links a public folder to a storage folder, a common setup in web projects.
is_link('report-link.txt')Checks that the new path really is a symbolic link.

The first argument is the target (what you point to) and the second is the link (the shortcut you create). On Windows, creating symbolic links needs administrator rights or Developer Mode, and symlink() may be unavailable on some shared hosts.

Library declarations
PHP
symlink(string $target, string $link): bool

Parameters

ParameterDescription
targetRequired. The path that the link will point to.
linkRequired. The path of the new link. It must not already exist.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Create a Symbolic Link

Create a file, make a link to it, and check that the link exists.

PHP
<?php
$dir    = sys_get_temp_dir();
$target = $dir . '/sym_target.txt';
$link   = $dir . '/sym_link.txt';

file_put_contents($target, 'Hello');

if (symlink($target, $link)) {
    echo 'Link created', PHP_EOL;
}

echo 'Is a link: ', is_link($link) ? 'yes' : 'no', PHP_EOL;

unlink($link);
unlink($target);

How It Works

  1. file_put_contents() creates the real file that the link will point to.
  2. symlink() takes the target first and the new link second.
  3. is_link() confirms the new path is a symbolic link and not a normal file.
  4. unlink() removes the link and then the target to clean up.

Example 2 — A Link That Already Exists

symlink() fails if the link path is already used, so the second call returns false.

PHP
<?php
$dir    = sys_get_temp_dir();
$target = $dir . '/sym_target2.txt';
$link   = $dir . '/sym_link2.txt';

file_put_contents($target, 'Hello');
symlink($target, $link);

if (@symlink($target, $link)) {
    echo 'Created again', PHP_EOL;
} else {
    echo 'Second call failed: link already exists', PHP_EOL;
}

unlink($link);
unlink($target);

How It Works

  1. The first symlink() call creates the link.
  2. The second call uses the same link path, which is now taken.
  3. symlink() returns false, and @ hides the PHP warning.
  4. A friendly message explains what happened. You could check file_exists() or is_link() before creating the link.

Example 3 — Read Through the Link

Read the file using the link path and use readlink() to see where it points.

PHP
<?php
$dir    = sys_get_temp_dir();
$target = $dir . '/sym_target3.txt';
$link   = $dir . '/sym_link3.txt';

file_put_contents($target, 'Hello');
symlink($target, $link);

echo file_get_contents($link), PHP_EOL;
echo 'Points to: ', basename(readlink($link)), PHP_EOL;

unlink($link);
echo 'Target still exists: ', file_exists($target) ? 'yes' : 'no', PHP_EOL;
unlink($target);

How It Works

  1. file_get_contents() on the link returns the content of the target file.
  2. readlink() returns the path stored in the link and basename() shortens it.
  3. unlink() on the link removes only the shortcut.
  4. file_exists() proves the real file is still there.

Frequently Asked Questions

It creates a symbolic link, a shortcut that points to another file or folder, without copying the data.
The target comes first and the link name comes second. The target is what you point to and the link is the new shortcut.
Windows needs administrator rights or Developer Mode to create symbolic links. Without these, the function returns false.
symlink() creates a symbolic link that points to a path. link() creates a hard link, which is another name for the same file data on the same disk.
Use unlink() on the link path. This removes only the link and leaves the target untouched.

Next: tempnam()

tempnam() creates a new empty file with a unique name in a folder you choose and returns t.

tempnam() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful