Something like an INI editor or a DelimitedText-Editor

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbie01.php
Forum Newbie
Posts: 1
Joined: Tue Oct 20, 2009 5:40 pm

Something like an INI editor or a DelimitedText-Editor

Post by newbie01.php »

Hi all,

Am very, very, very new to PHP and not sure if I should be posting this to a Javascript or PHP forum so I posed the question on both FORUM.

Basically, I am looking for somethin :cry: :cry: g like an INI editor of sort that I can use to let users modify delimited files which are in actual fact my INI file for my scripts on a UNIX box:

Below is an example of what the delimited file may look like:

Servername:OS:IPAddress:TypeofServer:Description:
SERVER01:AIX:1.2.3.1:PROD:Online Server:
SERVER02:Solaris:1.2.3.2:DEV:Online Dev Server:
SERVER03:Linux:1.2.3.3:TEST:Online Test Server:
SERVER04:Windows:1.2.3.4:PROD:Timesheet PROD Server:
SERVER05:HP:1.2.3.5:PROD:Candidate PROD Server:

I want to be able to provide the user with the list of directory on the server, and when they click on one of the files on the directory listing, it should allow them to load the file in some kind of form based web page and then they can make the changes and save the file. At the moment, users are going to the server and modifying the file manually via UNIX's vi and it is very prone to errors.

Does anyone have an example of what I am wanting to do?

Any suggestion will be very much appreciated.

Thanks in advance.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Something like an INI editor or a DelimitedText-Editor

Post by Weiry »

I actually wrote a script just for a purpose like this in one of the open source projects im working on.
I wanted to make a semi-command line interface for displaying files in a folder.

You can use this code, and just modify what you need. This only lists files from the directory specified by $folder though.

Code: Select all

<?php
#   List Files Console Command
function listFiles($server,$folder){
    if(!isset($server) || !isset($folder)){
        return false;
    }else{
    //----------------------------------------------
    // Open the specified SERVER FILE STRUCTURE file
        $file = "{$server}.sfs"; 
        $file_handle = fopen($file, "r"); 
        $file_contents = fread($file_handle, filesize($file)); 
        fclose($file_handle);
        $sfs = explode("\n", $file_contents);
    // Store SFS into an array and close file
    //----------------------------------------------
    
    //  Go through the file and remove any commented lines
        foreach($sfs as $row){
            if(strpbrk($row,"#")){
                array_splice($sfs,0,1);
            }
        }
        // remove the blank line remaining
            array_splice($sfs,0,1);
        
    //  Go through each line in the file and look
    //  for the matching directory. Once found,
    //  split the files in that directory into
    //  an array.
        foreach($sfs as $dir){
            if(substr($dir,0,3)==$folder){
                //remove the first 4 characters
                // they are folder data
                    $dir = substr_replace($dir,"",0,4);
                //remove the last 3 characters
                // they finish the line.
                    $dir = substr_replace($dir,"",strlen($dir)-3);
                //add all the files in the directory into an array
                // using the : delimiter
                $files = explode(":",$dir);
            }
        }
        $newFileArray = array();
        array_push($newFileArray,"Files in directory: {$folder}\n");
        foreach($files as $file){
            array_push($newFileArray,"-> {$file}\n"); // add file name to array( -> filename.txt ) you could add a link here
        }
        return $newFileArray;
    }
}
?>
This is an example of the file structure.
The name of the file is <ServerName>.sfs

Code: Select all

# ServerName.sfs
sys[kernel.sys:os.sys:accounts.sys:security.sys:crcdisk.sys];
usr[admin];
pub[data-Nin-115589:data-Nin-01559:app-Nin-22234];
log[log.dat];
bin[];
 
Using that, should give you a print out of the input $folder located in the input $server file.

From there you could change the code slightly where i mentioned you could add a link in, then send that to a php page which loads the data from file <filename> which would be located in your $_GET or $_POST if you wanted a little more work.

Also, this is how you use the above function:

Code: Select all

 
$fileList= listFiles($currentServer,$currentFolder);
foreach($fileList as $file){
    print "{$file}";
}
 
But if you stored all your servers in a single file, then you could change the opening of the file from a variable to a set file, then do a check on the server and folder rather than just the folder.
Post Reply