Thursday, 30 September 2010

How to Create Symbolic Links (Symlink) Using php

On Unix systems,

a symbolic link is a special type of file that contains a reference to another file or directory, any operation (read or write) on the symbolic link will behave as if operating directly on the refererce file.

If you have Unix shell access, creating a symbolic link is quite easy, you can use below command to implement one:

ln -s target_filename symlink_filename
ln -s /home/username/public_html/directory1 /home/username/public_html/directory2

But, here we will discuss about some very special cases in the real world that you need to create a symbolic link without a shell access. Below is one example:

A lot of people host their web site in a shared web hosting environement, and most of the shared web hosting vendors will not give you shell access for security reason. So, how can you create a symbolic link as you need?

The way we use here is to write a simple php script.

Create Symbolic Links via PHP

In php, there is a function call symlink(), which will allow you create a symbolic link. And below are the steps you can follow:

  • Edit, copy & paste the following line of code into a PHP (.php) file and upload it to the root directory on your server (i.e., the directory that contains “public_html”):




Notes: Please replace username, sourcedirectory, and symbolic_link according to your server environment

  • Next, trigger the function by calling the PHP file via your browser. If you see a blank page, you are good to go — the symbolic link has been created according to the specified parameters. To create another symbolic link, simply edit the parameters and repeat the process.3


To Disable the Link :-

No comments:

Post a Comment