Fatal error: Cannot redeclare file_put_contents()

Posted on 2008-11-11
Last Modified: 2013-11-05
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;
Question by:karakash
    LVL 10

    Expert Comment

    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.
    LVL 27

    Accepted Solution

    karakash, that's a name reserved for a PHP function:
    LVL 10

    Expert Comment

    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);

    Author Comment

    changing the function name giving the same error.
    LVL 10

    Expert Comment

    Check to see if your function is within a loop.

    If it is, then take it out of the loop.
    LVL 10

    Assisted Solution

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now