How to Create Symbolic Links (Symlink) Using php

What’s Symbolic Link

In 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.

Create Symbolic Link Through Linux Shell

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

ln -s target_filename symlink_filename

 

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”):

       <?php symlink(‘/home/username/public_html/sourcedirectory, ‘/home/username/public_html/symbolic_link); ?>

      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.

Web Host Allows PHP to Create Symbolic Links

If you host web sites in a shared Hosting, you might meet an issue that the web host disable PHP to create Symbolic Link. The reason is various but normally you will be told with a general one as “Security Concerns”, which is really not that true. Some top level hosting, like Bluehost, will give you the permission to create Symbolic Links through PHP. And we have tested all above scripts in Bluehost server.

Bluehost is one of the best web hosting in the industry, who is well-known for its professional and fast Linux web hosting. Its hosting solution comes very rich-features to help people setup a web site easily. It also guarantee 99.9% uptime and anytime money. Bluehost hosting solution is perfect for individuals and small businesses. It’s not promoting its hosting service at 50% off, after which the price will be $3.95/month. To know more about Bluehost, please check out our latest Bluehost review or visit http://www.bluehost.com directly.

bluehost-640