Occassional Config Saving Issues

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.


<?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 . . .

Nope... can't spot your problem buddy.

Hi kilishan,

Sorry I'm not able to see what the problem could be but thought I would reply in case the solution I came up with yesterday helps.


I'm presuming you would use this function to allow an admin to add config items from a control panel. I had a similar need, but opted for storing the config items in a db and dynamically creating when needed.

Hope that helps, even if just a little.


@helmutbjorg, thanks for looking the code over.

@George36, thanks for the pointer. It's not what I'm looking for, though it's a pretty nice solution.

This is for a personal project of mine. I've always that a CMS that worked with CI would be an 'ideal' situation, and one that I have yet to see. What I mean is that the CMS simply provides an interface to the existing CI config settings, can create basic controllers/models to build the urls of the site, and allows editing of view files to edit the pages themselves. To me, it seems like this would be the way to get the best performance possible, plus, it would give the end-user enough power to do what they need once some modules are built (like a blog, forums, etc), while allowing the developer the full power they needed to go in and edit the actual code.

Consider it a proof of concept Smile Just in the beginning stages, and this occasional glitch is driving me batty because I haven't yet determined a consistent cause. Then again, it's a personal project and time allotted for it is pretty slim at the moment.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.