How can I change the target of a symlink with PHP? Thanks.
A symbolic link contains a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the "target". The symbolic link is a second file that exists independently of its target.
Ln Command to Create Symbolic Links By default, the ln command creates a hard link. Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists.
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.
You can delete the existing link using unlink function and recreate the link to the new target using the symlink function.
symlink($target, $link);
.
.
unlink($link);
symlink($new_target, $link);
You need to do error checking for each of these.
PHP can execute shell commands using shell_exec
or the backtick operator.
Hence:
<?php
`rm thelink`;
`ln -s /path/to/new/place ./thelink`;
This will be run as the user which is running the Apache server, so you might need to keep that in mind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With