Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the target of a symlink with PHP

Tags:

linux

php

symlink

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

like image 652
tau Avatar asked Mar 23 '10 04:03

tau


People also ask

What is symlink target?

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.

How do you force a symbolic link?

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.

Is symlink PHP?

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.


2 Answers

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.

like image 95
codaddict Avatar answered Oct 08 '22 15:10

codaddict


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.

like image 3
nickf Avatar answered Oct 08 '22 14:10

nickf