Louis
Louis

Reputation: 73

Symlink Error: PHP Warning: symlink(): File exists

Good day,

Could someone please assist / advise as I am trying to create a symlink between the images folder in the public_html directory and the images folder in the App directory. When running the symlink file, the message displays: "Symlink process successfully completed", but I receive an error log stating:

PHP Warning: symlink(): File exists in /home/property/public_html/symlink.php on line 5

The code that I am using in order to try and create the symlink are:

<?php

$targetFolder = '/home/property/app/images';
$linkFolder = '/home/property/public_html/images';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';

?>

Upvotes: 1

Views: 5032

Answers (3)

Rahid Alekberli
Rahid Alekberli

Reputation: 1

It looks from the previous installations symlink for SquirrelMail is linked to RoundCube. Unlinking/removing solved in my case:

root@www:~# ll /usr/share/squirrelmail 

/usr/share/squirrelmail -> /usr/share/roundcube/

Upvotes: 0

Hunman
Hunman

Reputation: 155

The symlink() documentation states that this is the expected behaviour if an error happens: https://www.php.net/manual/en/function.symlink.php#refsect1-function.symlink-errors

Even though error supression is a double edged sword, you can probably use it here if you check for the output of symlink() in a similar fashion:

if (@symlink($targetFolder, $linkFolder)) { // The @ suppresses the error
    echo 'Symlink process successfully completed';
} else {
    echo 'Symlink process failed';
    return 1; // You should probably exit with a non-zero status code if that is your whole script.
}

Alternatively

// die() will run only if symlink() returns false
@symlink($targetFolder, $linkFolder) || die('Symlink process failed');

// If we reach this point, die() didn't run thus symlink was successful
echo 'Symlink process successfully completed';

Upvotes: 0

Louis
Louis

Reputation: 73

It seems like the error was caused in the public_html folder that already contained an "images" folder. After removing the images folder inside the public_html directory and executing the symlink script, the symlink was created successfully.

Upvotes: 1

Related Questions