Matt Hutch
Matt Hutch

Reputation: 463

Can't get my php to add to my text document?

I'm currently working on a project for college but i'm having issues with it. I have two pages with a form on each which includes three text fields (des,act,date) I'm trying to make it so that it will add to the text document the information from the forms but at the minute all it is doing is overwriting it. Anyone know how to solve this?

Page 1

    if (isset($_GET['logout'])){
        session_destroy();  
    }

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }

    //Send Data
    $content = 'OBSERVATION'."\r\n".'Breif Description: '.$_POST['des1']."\r\n".'Agreed Action: '.$_POST['act1']."\r\n".'Close Date: '.$_POST['date1']."\r\n";

    if (isset($_POST['submit'])){
        $myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
        fwrite($myFile, $content);
        fclose($myFile);
        header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
    }



?>

Page 2

    if (isset($_GET['logout'])){
        session_destroy();  
    }

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }

    //Send Data
    $content = "\r\n\r\n".'GOOD PRACTICES'."\r\n".'Breif Description: '.$_POST['des2']."\r\n".'Agreed Action: '.$_POST['act2']."\r\n".'Close Date: '.$_POST['date2']."\r\n";

    if (isset($_POST['submit'])){
        $myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
        fwrite($myFile, $content);
        fclose($myFile);
    }

?>

Upvotes: 1

Views: 42

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92874

Use file_put_contents function with FILE_APPEND flag.

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

FILE_APPEND : If file filename already exists, append the data to the file instead of overwriting it.

...

    if (isset($_POST['submit'])) {
       file_put_contents("Observation.txt", $content, FILE_APPEND);
       header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
       exit;
    }

...

http://php.net/manual/en/function.file-put-contents.php

Upvotes: 1

jilesh
jilesh

Reputation: 436

Use file_put_content

if (isset($_POST['submit'])) {
 file_put_contents("Observation.txt", $content, FILE_APPEND);
... your code here
}

Here third parameter in file_put_content "FILE_APPEND" will append your file every time with new content in your previous code it was overwrite one content with another because of same name so if you want to do it on that way than you want to set different name of both file.

Here file_put_content function url : http://php.net/manual/en/function.file-put-contents.php

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212452

fopen() with a mode of 'w'

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

fopen() with a mode of 'a'

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.

Upvotes: 1

Related Questions