Results 1 to 2 of 2

Thread: Ini #2

  1. #1

    Thread Starter
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Ini #2

    Fuction

    PHP Code:
    <?php

    //****************************************************************************************

    //INIFunc.php

    //Freeware functions

    //Copyright 2001 Nicola Viganò <[email protected]>

    //THIS CODE IS DISTRIBUITED "AS IT IS", WITHOUT WARRANTY

    //****************************************************************************************

    //Instructions

    //

    //Functions use any of these parameters:

    //    $section    : section name

    //    $key(s)        : key(s) name

    //    $value        : value

    //    $default    : default value if $key can not be found

    //    $file_name    : name of the file

    //    

    //Available functions:

    //

    //    write_key($section, $key, $value, $file_name)

    //        Writes a key and its value under a certain section.

    //        It creates the file if it doesn't exist.

    //

    //    write_section($section, $keys, $file_name)

    //        Writes an entire section and its keys.

    //        It creates the file if it doesn't exist.

    //        $keys must be a single dimension array, structured as: $keys[$key]=$value.

    //        eg:    $keys["Key1"]="Value1";

    //            $keys["Key2"]="Value2";

    //            ...

    //        eg:    $keys = array( "Key1" => "Value1", "Key2" => "Value2");

    //

    //    delete_key($section, $key, $file_name)

    //        Deletes a key

    //

    //    delete_section($section, $file_name)

    //        Deletes an entire section.

    //

    //    read_key($section, $key, $default, $file_name)

    //        Reads a certain value.

    //        It returns $default if the value or the file do not exist.

    //

    //    read_section($section, $default, $file_name)

    //        Reads an entire section.

    //        It returns a single dimension array, structured as: $keys[$key]=$value.

    //        It returns $default if the section or the file do not exist.

    //        eg: If $keys is the returned value, you can show values with the following code:

    //            foreach ($keys as $key=>$value)

    //                echo "$key => $value<br>";





    function write_key($section$key$value$file_name)

    {

        if (!
    file_exists($file_name))

        {    
    //File not found

            
    $content "[$section]\n";

            
    $content .= "$key=$value";    

        }

        else

        {

            
    $content read_ini($file_name);

            

            
    //Find section

            
    $currentsection find_section($content$sectionFALSE);

            if (!isset(
    $currentsection))

            {

                
    //Section not found

                
    $content .= "\n\n[$section]\n";

                
    $content .= "$key=$value";

            }

            else

            {

                
    //Find key

                    
    $currentkey find_key($currentsection$keyFALSE);

                    if (!isset(
    $currentkey))

                {    
    //Key not found

                    
    $newsection $currentsection."\n$key=$value";

                    
    $content str_replace($currentsection$newsection$content);

                }

                    else

                    {

                    
    $newsection str_replace($currentkey"$key=$value"$currentsection);

                    
    $content str_replace($currentsection$newsection$content);                

                }

            }

        }

        
    write_ini ($content$file_name);

    }



    function 
    write_section($section$keys$file_name)

    {

        if (!
    file_exists($file_name))

        {    
    //File not found

            
    $content "[$section]\n";

            foreach (
    $keys as $key=>$value)

                
    $content .="$key=$value\n";

        }

        else

        {

            
    $content read_ini($file_name);

            

            
    //Find section

            
    $currentsection find_section($content$sectionFALSE);

            if (!isset(
    $currentsection))

            {

                
    //Section not found

                
    $content .= "\n\n[$section]\n";

                foreach (
    $keys as $key=>$value)

                    
    $content .="$key=$value\n";

            }

            else

            {

                
    $newsection $currentsection;

                
    //Find key

                
    foreach ($keys as $key=>$value)

                {

                    
    $currentkey find_key($currentsection$keyFALSE);

                        if (!isset(
    $currentkey))

                    {    
    //Key not found

                        
    $newsection .= "\n$key=$value";

                    }

                        else

                        {

                        
    $newsection str_replace($currentkey"$key=$value"$newsection);

                    }

                }

                
    $content str_replace($currentsection$newsection$content);

            }

        }

        
    write_ini ($content$file_name);

    }



    function 
    delete_key($section$key$file_name)

    {

        if (!
    file_exists($file_name))

            return (
    FALSE);

        

        
    $content read_ini($file_name);

        

        
    //Find section

        
    $currentsection find_section($content$sectionFALSE);

        if (!isset(
    $currentsection))

            return (
    FALSE);

        

        
    //Find key

        
    $currentkey find_key($currentsection$keyFALSE);

        if (!isset(
    $currentkey))

            return (
    FALSE);

                      

        
    $newsection str_replace("\n".$currentkey""$currentsection);

        
    $content str_replace($currentsection$newsection$content);                



        
    write_ini ($content$file_name);

        

        return (
    TRUE);    

    }



    function 
    delete_section($section$file_name)

    {

        if (!
    file_exists($file_name))

            return (
    FALSE);

        

        
    $content read_ini($file_name);

            

        
    //Find section

        
    $currentsection find_section($content$sectionFALSE);

        if (!isset(
    $currentsection))

            return (
    FALSE);

        

        
    $content str_replace($currentsection""$content);



        
    write_ini ($content$file_name);



        return (
    TRUE);

    }



    function 
    read_key($section$key$default$file_name)

    {

        if (!
    file_exists($file_name))

            return(
    $default);

        

        
    $content read_ini($file_name);

            

        
    //Find section

        
    $currentsection find_section($content$sectionTRUE);

        if (!isset(
    $currentsection))

            return(
    $default);

        
    //Find key

            
    $value find_key($currentsection$keyTRUE);

            if (!isset(
    $value))

            return(
    $default);

            

            return (
    $value);    

    }



    function 
    read_section($section$default$file_name)

    {

        if (!
    file_exists($file_name))

            return(
    $default);

                

        
    $content read_ini($file_name);

        

        
    //Find section

        
    $currentsection find_section($content$sectionTRUE);

        if (!isset(
    $currentsection))

            return(
    $default);

        

        
    //Get keys list

            
    $lines explode("\n" $currentsection);

        foreach (
    $lines as $line)

        {

            list(
    $key,$value)=explode('=',$line);

            
    $keys[$key]=$value;

        }

            return (
    $keys);    

    }



    //************************************************************************************

    //Private functions

    //************************************************************************************

    function read_ini ($file_name)

    {

        
    clearstatcache();

        
    $file fopen($file_name"r");

        
    flock($fileLOCK_SH); 

        
    $content trim(@fead($filefilesize($file_name)));

        
    flock($fileLOCK_UN); 

        
    fclose ($file);

        return (
    $content);

    }



    function 
    write_ini ($content$file_name)

    {

        
    $file fopen($file_name"w");

        
    flock($fileLOCK_EX);

        
    fwrite($file$content);

        
    flock($fileLOCK_UN);

        
    fclose ($file);

    }



    function 
    find_section ($content$section$just_keys)

    {

        
    $section "[$section]\n";

        
    $currentsection strstr($content$section);

        if (!
    $currentsection)

            return(
    $empty);

        

        
    $endpos strpos($currentsection"\n\n");

        if (!
    $endpos)

            
    $endpos strpos($currentsection"\n[");

        if (!
    $endpos)

            
    $endpos strlen($currentsection);

        

        if (
    $just_keys)

            
    $currentsection trim(substr($currentsectionstrlen($section), $endpos-strlen($section)));    

        else

            
    $currentsection trim(substr($currentsection0$endpos));            

        

        return (
    $currentsection);

    }



    function 
    find_key ($currentsection$key$just_value)

    {

            
    $key .= "=";

            
    $currentkey strstr($currentsection$key);

            if (!
    $currentkey)

            return(
    $empty);

            

            
    $endpos strpos($currentkey"\n");

            if (!
    $endpos)

            
    $endpos strlen($currentkey);    

        

        if (
    $just_value)

            
    $currentkey trim(substr($currentkeystrlen($key), $endpos-strlen($key)));

        else

            
    $currentkey trim(substr($currentkey0$endpos));

        

        return (
    $currentkey);

    }

    ?>

    My code
    PHP Code:
    <?
    include("inifunc.php");
    $value = read_key("Index", "Loads", "default value", "test.txt");
    PRINT("$value");
    ?>
    test.txt
    Code:
    [Index]
    Loads=1

    Problem:
    Nothing happens!

  2. #2
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: Ini #2

    ahem... are you forgetting some "squiggly brackets"?
    PHP Code:
    function read_key($section$key$default$file_name



        if (!
    file_exists($file_name)) 

            return(
    $default); 

         

        
    $content read_ini($file_name); 
    look at the if... are you missing something?
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width