PHP Script to Create Local and LAN Symlinks

Discussion in 'PHP' started by sirgogo, Jun 23, 2011.

  1. #1
    Hi all,

    New here, so here goes. Trying to make a PHP script to create symbolic links on my server to other in-house servers. I want to be able to give the script input from a file, say sources_add.txt, one line per row, separated by tabs or "||", and the line terminated by "!". Two arguments per row, target and destination. I've been looking around the internet, but as I'm new to PHP I turned to these forums for help.

    This is a sad attempt, but here's my code so far:
    ------------------------------------------------------------
    
    <?php
    
    $fp = fopen('sources_add.txt', 'r') OR die("can't open file!");
    
    while($row =(fgetcsv($rssc,2000)))
    
    {
    $start	= 0;
    
    $end 	= strpos(!);
    $tar_end= strpos('||')
    
    $link_st= tar_end++;
    
    $target	= substr($row, start, tar_end);
    
    $link	= substr($row, link_st, end);
    $creation = bool symlink($target, $link);
    if ($creation)
    
    	//print('success!')
    
    else
    	//print('failed! check that paths exist and link has not already been made.')
    
    
    
    }
    
    ?>
    
    Code (markup):
    Any help/advice/howtos would be useful. Thanks!
     
    sirgogo, Jun 23, 2011 IP
  2. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Some advice I'd give you for this:

    Instead of fopen/fclose, use file_get_contents and put it into a variable.
    Read through the variable and explode it by the delimiter "!".
    Foreach the array given from explode() and do the command with it.

    Also post some example data so I can better understand what you're wanting done. So far, you've done an excellent job for what you want done.
     
    JoelLarson, Jun 24, 2011 IP
  3. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply Joel. Sorry for the late reply, I was away for the weekend.

    I'm thinking I could use explode to create an array of data targets and links?
    
    array explode ( string '|', $row)
    
    Code (markup):
    Sample data: source_add.txt
    (target | link !)
    /srv/docs/policy.pdf|/var/www/html/docs/!
    Code (markup):
    Essentially, I want to create a PHP script that can be called to create the desired symlinks. I was hoping for it to be like a batch file I could run via a web browser to read the txt file and clear the lines of text as it successfully creates the links.
     
    sirgogo, Jun 26, 2011 IP
  4. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could explode by the !, then trim the extra \n off the front, and do a foreach to the exploded ! array. Then, you explode each | and do something like:

    shell_exec("ln -s {$paths['0']} {$paths['1']}");
    PHP:
    --

    EDIT:

    I forgot there is a symlink command in PHP:
    symlink($paths['0'], $paths['1']);
    PHP:
     
    JoelLarson, Jun 26, 2011 IP
  5. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sweet, thanks dude. I'll give that a shot.
     
    sirgogo, Jun 27, 2011 IP