Welcome Guest, Not a member yet? Register   Sign In
Occassional Config Saving Issues
#1

[eluser]kilishan[/eluser]
I'm working on a extended Config class that actually allows for saving items to the original config files. I've got it working for both config files and the database config files (saving dev, test, and prod server settings). However, every once in a while it decides to write the config file without the first line (the one with the PHP opening tag) so the config file fails to function when read and breaks the program. Obviously, this is a deal-breaker.

Below is the current save() method. Does anyone have any idea what might be causing this?

Also, if you know of a better way to find/replace the existing lines with the config setting on it, I'd love to hear it. Currently this one feels a bit hackish, though it does seem to work.

Thanks

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

. . . other functions here . . .
    
    public function save($file='', $settings=null)
    {
        if (empty($file) || !is_array($settings    ))
        {
            return false;
        }
        
        if (!file_exists(APPPATH.'config/'.$file.EXT))
        {
            return false;
        }
        
        // Load the file so we can loop through the lines
        $contents = file_get_contents(APPPATH.'config/'.$file.EXT);
        
        foreach ($settings as $name => $val)
        {
            // Is the config setting in the file?
            $start = strpos($contents, '$config[\''.$name.'\']');
            $end = strpos($contents, ';', $start);
            
            $search = substr($contents, $start, $end-$start+1);
            
            $contents = str_replace($search, '$config[\''.$name.'\'] = \''. $val .'\';', $contents);
        }
        
        // Backup the file for safety
        $source = APPPATH . 'config/'.$file.EXT;
        $dest = APPPATH . 'config/'.$file.EXT.'.bak';
        copy($source, $dest);
        
        // Write the changes out...
        $result = file_put_contents(APPPATH.'config/'.$file.EXT, $contents, LOCK_EX);
        
        if ($result === FALSE)
        {
            return false;
        } else {
            return true;
        }
    }
    
    //---------------------------------------------------------------
    
. . . other functions here . . .


Messages In This Thread
Occassional Config Saving Issues - by El Forum - 02-01-2010, 10:54 PM
Occassional Config Saving Issues - by El Forum - 02-02-2010, 01:32 AM
Occassional Config Saving Issues - by El Forum - 02-02-2010, 07:09 AM
Occassional Config Saving Issues - by El Forum - 02-02-2010, 10:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB