Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1197
  • Last Modified:

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);
        fclose($f);
       return $bytes_written;
}
}
0
karakash
Asked:
karakash
  • 4
2 Solutions
 
ray-solomonCommented:
file_put_contents() is a core php function name.
http://us2.php.net/file_put_contents

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.
0
 
ddrudikCommented:
karakash, that's a name reserved for a PHP function:
http://us3.php.net/manual/en/function.file-put-contents.php
0
 
ray-solomonCommented:
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);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

If it is, then take it out of the loop.
0
 
ray-solomonCommented:
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.

http://us.php.net/manual/en/function.require.php
http://us.php.net/manual/en/function.include.php
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now