Results 1 to 7 of 7

Thread: [RESOLVED] writing to file in php

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Resolved [RESOLVED] writing to file in php

    hi all i have made a proram that reports information to a txt file so i can extract it on a web page..
    the problem i have is i want to add txt to the file using php for reporting back to the program

    this is the text file its set in lists for the arrays for reading

    Code:
    [computers]
    0=ATOMVIDEOBACK1
    1=MINIVAULT
    2=ATOMVAULT1
    3=ATOMVIDEOBACK2
    4=BIGME
    5=VMACHINE1
    [comNumb]
    comNumb=6
    i want to add the following to the txt file for reading on the program, i want to add the text using php but i also need it to check to see if the text already exist in the file


    Code:
    [info]
    ATOMVIDEOBACK1=got
    MINIVAULT=got

    finished file
    Code:
    [computers]
    0=ATOMVIDEOBACK1
    1=MINIVAULT
    2=ATOMVAULT1
    3=ATOMVIDEOBACK2
    4=BIGME
    5=VMACHINE1
    [comNumb]
    comNumb=6
    
    [info]
    ATOMVIDEOBACK1=got
    MINIVAULT=got

    thanks for anyone that can help
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  2. #2
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: writing to file in php

    To check if a string exists
    PHP Code:
     $chkStr 'This is a test string';    
        if(
    strpos(file_get_contents("filename.txt"), $chkStr) !== false) {
            echo 
    'String already exists';
        } else {
            echo 
    'String dont exists';
        } 
    And how to write string to file
    PHP Code:
        $str 'This is a test string';
        
    $fh fopen('filename.txt','a+'); // a+ sets the pointer at end of file, more mode parameters at php.net
        
    fwrite($fh,$str);
        
    fclose($fh); 
    FORZA ROSSONERI! CAMPIONI!!!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Question Re: writing to file in php

    so i managed to work out if aray exists

    Code:
    //set array
        $str = '[info]';
        $path = "computers.ini";
    
        $file_arrayc = parse_ini_file($path, true);
        if (array_key_exists('info', $file_arrayc)) {
                echo "got array";
            } else {
                $fh = fopen($path,'a+'); // a+ sets the pointer at end of file, more mode parameters at php.net
                fwrite($fh,$str);
                fclose($fh);
          }
       //set array
    thanks for your help Rossonero

    im not sure how to update the array in the file... a little stuck here
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  4. #4
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: writing to file in php

    I think the easiest is to read the file into a new array, modify it and then put it back in the file

    PHP Code:
    $newArray file('filename.txt'); // Reads file to an array 
    FORZA ROSSONERI! CAMPIONI!!!

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: writing to file in php

    See if this is what you are looking for:

    PHP Code:
    <?php

        $section     
    'info';        //section name to look for
        
    $file         "data.ini";    //INI file

        
    $data     parse_ini_file($filetrue);    //parse the INI file returning an array
        
        
    if ( array_key_exists$section$data ) )    //check whether the section name (ie. key in the array) exists
        
    {
            echo 
    "Section already exists in the INI file!";
        } 
        else     
    //if it doesn't exists, we are going to insert a new element into the array, with the section name as "info", with an array containing key=>value pairs
        
    {
            
    $data$section ] = array(
                                    
    'ATOMVIDEOBACK1'     => 'got',
                                    
    'MINIVAULT'         => 'got'
                                
    );
            
            
    write_php_ini$data$file );    //writes back to the file
            
            
    echo 'Updated INI file!';
        }
        
        
        
        
        
    //------------- FUNCTIONS ------------------- More info: http://www.php.net/manual/en/function.parse-ini-file.php#94414
        
        
    function write_php_ini($array$file)
        {
            
    $res = array();
            foreach(
    $array as $key => $val)
            {
                if(
    is_array($val))
                {
                    
    $res[] = "[$key]";
                    
    //foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');    //Includes double quotes for string values. In your case, it seems like you don't need it(based on the examples). So I commented it and wrote the second line
                    
    foreach($val as $skey => $sval$res[] = "$skey = ".$sval;
                }
                
    //else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');                                        //Includes double quotes for string values. In your case, it seems like you don't need it(based on the examples). So I commented it and wrote the second line
                
    else $res[] = "$key = ".$val;
            }
            
    safefilerewrite($fileimplode("\r\n"$res));
        }

        function 
    safefilerewrite($fileName$dataToSave)
        {    
            if (
    $fp fopen($fileName'w'))
            {
                
    $startTime microtime();
                do
                {            
    $canWrite flock($fpLOCK_EX);
                   
    // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
                   
    if(!$canWriteusleep(round(rand(0100)*1000));
                } while ((!
    $canWrite)and((microtime()-$startTime) < 1000));

                
    //file was locked so now we can store information
                
    if ($canWrite)
                {            
    fwrite($fp$dataToSave);
                    
    flock($fpLOCK_UN);
                }
                
    fclose($fp);
            }
        }



    ?>
    I have borrowed two functions from here: http://www.php.net/manual/en/functio...file.php#94414

    And changed two of the lines in one of the function, to remove the insertion of double quotes for string values. Because in your example, it doesn't look like you need double quotes wrapped around the string values!


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: writing to file in php

    so i changed the info part to the computer communicating to.

    original
    PHP Code:
    [info]
    ATOMVIDEOBACK1=got
    MINIVAULT
    =got 
    new
    PHP Code:
    [MINIVAULT]
    0=got
    1
    =more bits
    2
    =more bits
    3
    =more bits

    [ATOMVIDEOBACK1]
    0=got
    1
    =more bits
    2
    =more bits
    3
    =more bits 
    i have found this better for posting more information from the page to the computer program that collects the information

    i now need to keep what is in the array for that computer but im stucking with merging the arrays together in adding it back into the file

    PHP Code:
    $WW count($data[$Com]) ;// $data[$Com] is the original array from the file count
            
    $data$section ] = array(

                                  for (
    $i=1$i<=$WW$i++)
                                            {
                                              if   (
    $data[$Com][$i-1] =="0"){// this is the part that has been changed
                                              
    '0'     => 'gotgggg',
                                              }else{
                                              
    $i-1     => $data[$Com][$i-1],
                                              }
                                            }
                                             );

            
    write_php_ini$data$file );    //writes back to the file 
    so what im trying todo above is load the ariginal array and count it. that bit works fine.
    then edit 0 section to anything i want and merge everything together and put it back into the file, but the merge art is not working..

    thanks for your help akhileshbc the code works perfect for what i need just need to try and solve this merging bit out..
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Smile Re: writing to file in php

    hi all
    i just wanted to say thanks to akhileshbc and Rossonero for helping me with this.. i solved my problem the code now works

    below is the finished snippet to change the information i needed
    PHP Code:
    $data[$Com][0] = 'googleeeee';
             
    write_php_ini$data$file );    //writes back to the file 
    for some crazzy reason i decided to load all the array into a new array and got it completely wrong. so started from scratch..
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

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