file_put_contents() Function for PHP 4

I needed to run code I wrote for PHP 5 on a PHP 4 installation. Everything worked except the file_put_contents function. I scoured the web for a good implementation of the function for PHP 4 and I came across phpied.com here: http://www.phpied.com/file_get_contents-for-php4/. And the site is run on WordPress! Nicely done!

Here is his code:

if (!function_exists('file_put_contents')) {
    function file_put_contents($filename, $data) {
        $f = @fopen($filename, 'w');
        if (!$f) {
            return false;
        } else {
            $bytes = fwrite($f, $data);
            fclose($f);
            return $bytes;
        }
    }
}

Leave a Reply