Cannot save JSON data with fwrite()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4dplane
Forum Newbie
Posts: 6
Joined: Wed Nov 25, 2009 11:51 am

Cannot save JSON data with fwrite()

Post by 4dplane »

Hi,

I am able to opening a cache file(.txt), decoding it as JSON and insert a new name&value. All of this works and I have checked many times that the final array is filled with the correct data. What I can't do is write the data to back to the cache file. I have error reporting on but I don't receive any and the the cache file is always the same, never updated. Even if I put "0" in fwrite the file is never updated

Code: Select all

 
***Method to open file and decode it into array******
function openCache(){
        //open the cache imaage into an array
        $this->allMediaHandle = fopen($this->allMediaCacheURL, 'r+') or die("can't open file");//channel info to open
        $this->openAllMediaCache = fgets($this->allMediaHandle);
        //decode into JSON creating a 2D array - dataToSearch
        $this->allMediaCache = json_decode($this->openAllMediaCache, true);
}
 
**** code snippet that inserts a new name&value
$this->allMediaCache[$i]['channel_id'] = $this->ChannelIDsAndMedia[$j][0];
 
****method to write to the cache file and close it******
function closeCache(){
        //echo $this->allMediaCache[$i]['channel_id'];
        fwrite($this->allMediaHandle, $this->allMediaCache);
        //$this->allMediaCache = json_encode($this->allMediaCache); // http://docs.php.net/manual/en/function.json-encode.php
        fclose($this->allMediaHandle);
}
4dplane
Forum Newbie
Posts: 6
Joined: Wed Nov 25, 2009 11:51 am

Re: Cannot save JSON data with fwrite()

Post by 4dplane »

Wow, it's my hosting service - I have to request that they turn on allow_url_fopen.
Post Reply