sarsar
sarsar

Reputation: 1561

writing to a text file

I have a form that write its values to a text file. My problem is that when a user submits a form it over writes the value from the last users submission. What changes would i need to make in order to have it record each users submission and not over write it each time.

// This just names the file
$target_filename = "usersubmit_f456sd4f56sd4f.txt";

// Create an empty buffer
$message = "";

// This gets all the form keys (names) and values
foreach ($_POST as $key => $value)
$message .= "$key: $value\n";

// Put the date in
$message .= date("F j, Y, g:i a");

// Open the file and write it out
$fp = @fopen($target_filename,"wt");
if ($fp != NULL) 
{
fputs($fp,$message);
fclose($fp);
}

Upvotes: 1

Views: 171

Answers (6)

xkeshav
xkeshav

Reputation: 54016

try instead

<?php
$file = 'usersubmit_f456sd4f56sd4f.txt';
// The new person to add to the file
 $person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>

file_put_contents

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

Upvotes: 0

Sanjeev Kumar Jha
Sanjeev Kumar Jha

Reputation: 1263

You can use this code:

<?php 
    $target_filename = "testFile.txt";
    $fh = fopen($target_filename, 'r');
    $theData = fread($fh, filesize($target_filename));

    $fh = fopen($target_filename, 'w') or die("can't open file");
    $message = $theData."\n";
    foreach ($_POST as $key => $value)
    $message .= "$key: $value\n";

    //Put the date in
    $message .= date("F j, Y, g:i a");
    fwrite($fh, $message);
    fclose($fh);
    ?>

Hope it works. Thanks

Upvotes: 1

Harsh
Harsh

Reputation: 2096

take this fopen() function or You can Go with php manual ......use this

// This just names the file
$target_filename = "usersubmit_f456sd4f56sd4f.txt";

// Create an empty buffer
$message = "";

// This gets all the form keys (names) and values
foreach ($_POST as $key => $value)
$message .= "$key: $value\n";

// Put the date in
$message .= date("F j, Y, g:i a");

// Open the file and write it out
$fp = fopen($target_filename, 'a'); 
if ($fp != NULL) 
{
fputs($fp,$message);
fclose($fp);
}

a: Append. Opens and writes to the end of the file or creates a new file if it doesn't exist

Modes   Description
r   Read only. Starts at the beginning of the file
r+  Read/Write. Starts at the beginning of the file
w   Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+  Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a   Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+  Read/Append. Preserves file content by writing to the end of the file
x   Write only. Creates a new file. Returns FALSE and an error if file already exists
x+  Read/Write. Creates a new file. Returns FALSE and an error if file already 

Upvotes: 0

Fender
Fender

Reputation: 3055

// Open the file and write it out
$fp = @fopen($target_filename,"wt");
if ($fp != NULL) 
{
    fseek($fp,0,SEEK_END);
    fputs($fp,$message);
    fclose($fp);
}

http://php.net/fseek

Upvotes: -1

ThiefMaster
ThiefMaster

Reputation: 318468

Open the file in append mode:

$fp = fopen($target_filename, 'a');

You could also simply use file_put_contents():

file_put_contents($target_filename, $message, FILE_APPEND);

Upvotes: 0

M.V.
M.V.

Reputation: 1672

Change

$fp = @fopen($target_filename,"wt");

TO

$fp = fopen($target_filename, 'a');  

You can find references HERE... When using a it means to append... When file doesn't exists this function will force creation...

Hope it helps!

Upvotes: 6

Related Questions