Jump to content

Recommended Posts

Hey up guys. For a little project I'm involved with I've been trying to write a pretty simple config file handler class. Everything works great except my "commitChanges()" method. Although this is part of a class the problem isn't OO (so it's not in the OO forum) ..

 

Here's the code:

 

public function commitChanges()
{
    // Ensure file is writable
    if (!is_writable($this->src)) $this->commitError('Config file un-writable!');
       
    // Detect error opening file
    if ($handle = fopen($this->src, 'w') === false) $this->commitError('Unable to open config file!');
        
    // Loop through each line of config file
    foreach ($this->config_arr as $key => $val)
    {
        // If a line has been altered...
        if (array_key_exists($key, $this->prop_name))
        {
            // ...update that line
            $val = $this->prop_name[$key] . '=' . $this->prop_value[$key];
        }

        print 'Writing: ' . $val . ' to file ... ';
        // Attempt to write to file and catch any errors
        if (fwrite($handle, $val) === false)
            print '[FAILED]';
        else
            print '[sUCCESS]';
        print '<br />';
    }
    fclose($handle);
        
    return true;
}

 

I'm not getting any errors except my own "[FAILED]" message for each line.

 

$this->src,

$this->config_arr,

$this->prop_name,

$this->prop_value

 

...are all defined no problem. There is no problem with the foreach () either. My config file has all the right permissions (put it to chmod 0777 just to be sure). The problem purely lies with fwrite(). Has anybody come across this kind of problem before, or have any ideas what the problem could be?

 

Cheers for any help! Adam

Link to comment
https://forums.phpfreaks.com/topic/134176-fwrite-problem/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.