Fatal error: Cannot redeclare file_put_contents()

Following function giving out error Fatal error: Cannot redeclare file_put_contents(). Can someone tell me what might be wrong with the statement? It was working fine in PHP4 but after upgrading server to php5 its giving the error.

function file_put_contents($n, $d, $flag = false) {
    $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
    $f = @fopen($n, $mode);
    if ($f === false) {
        return 0;
    } else {
        if (is_array($d)) $d = implode($d);
        $bytes_written = fwrite($f, $d);
       return $bytes_written;
Who is Participating?
ddrudikConnect With a Mentor Commented:
karakash, that's a name reserved for a PHP function:
file_put_contents() is a core php function name.

You just need to rename your custom function to something different, then update the references to that function in your code.

You should have got the same Fatal error in php4, so this means the display_errors was off in your php.ini or this script.
Or you can just use the core function instead since it does the same as your custom function.

file_put_contents($filename, $data, FILE_APPEND);
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

karakashAuthor Commented:
changing the function name giving the same error.
Check to see if your function is within a loop.

If it is, then take it out of the loop.
ray-solomonConnect With a Mentor Commented:
Your error message means the function is being called more than once during the execution of the script.

You can also check if your function is being included in your script more than once.
Sometimes this happens if you include multiple php scripts together into your main php script and one of those scripts is also calling the same function.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.