简体   繁体   中英

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';

?>

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';

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM