写入文本文件

时间:2011-05-25 06:24:47

标签: php

我有一个将其值写入文本文件的表单。我的问题是,当用户提交表单时,它会覆盖上次用户提交的值。我需要进行哪些更改才能记录每个用户提交的内容,而不是每次都写过。

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

6 个答案:

答案 0 :(得分:6)

更改

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

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

你可以找到引用HERE ...当使用a时它意味着追加......当文件不存在时,这个函数会强制创建...

希望它有所帮助!

答案 1 :(得分:1)

您可以使用此代码:

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

希望它有效。 感谢

答案 2 :(得分:0)

尝试改为

<?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

  

此功能与连续调用fopen()fwrite()fclose()以将数据写入文件相同

答案 3 :(得分:0)

a ppend 模式下打开文件:

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

您也可以使用file_put_contents()

file_put_contents($target_filename, $message, FILE_APPEND);

答案 4 :(得分:0)

拿这个fopen() function或者你可以跟php manual一起......用这个

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

aAppend。打开并写入文件末尾或创建新文件(如果不存在)

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 

答案 5 :(得分:-1)

// 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