Creating Symbolic/Softlinks Using PHP

PHP is a fantastic language and it has some very useful capabilities. I am currently developing a website that has a unique structure. Basically, I am using symbolic links to act as subdomains that all point to a single program. I am then using PHP’s built in $_SERVER array to find the HTTP host. From there I am extracting the first portion of the subdomain and using that to select the proper template and information from the database. This strategy allows each sub domain to appear as a unique site to search engines and visitors while only having one set of application code.

As part of the application I am required to automate the process of adding new entries to the system. To do this, I needed the ability to add softlinks or symbolic links whichever term you are accustomed to from my script. After doing some research I found a couple of ways to do it. PHP has a built in function called symlink() which was capable of handling the task. In similar fashion you could use the exec() command to achieve the same result. My final solution uses the symlink() function so I will show that code.

Adding a Symbolic/Softlink Link with PHP
(Note: Requires PHP has sufficient permissions to execute the command. If your script or your PHP install don’t have sufficient permissions this will not work.)

<?PHP
 
    //SET THE NAME OF THE LINK
    $link = "samplelink";
 
    //CREATE A SYMBOLIC LINK USING PHP
    symlink("/usr/local/www/htdocs/website.com/www/","/usr/local/www/htdocs/website.com/{$link}");
 
?>

Removing a Symbolic/Softlink using PHP

<?PHP
 
    SET THE NAME OF THE LINK 
    $link = "samplelink";
 
    //REMOVE THE SYMBOLIC LINK WE CREATED BEFORE
    unlink("/usr/local/www/htdocs/furnishingforyou.com/{$link}");
 
?>

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)