views:

53

answers:

2

How can I change the target of a symlink with PHP? Thanks.

A: 

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.

nickf
Shell commands should be avoided when there are built-in alternatives.
too much php
+5  A: 

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.

codaddict
+1 - this is much better than writing the shell stuff yourself.
nickf
okay, i was just seeing if there was a way around having to unlink and then recreate the symlink. thanks!
tau