English 中文(简体)
写入文本文件
原标题:writing to a text file
  • 时间: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
";

// 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);
}
问题回答

改变

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

$fp = fopen($target_filename,  a );  

您可以找到参考资料HERE…当使用a时,意味着附加…当文件不存在时,此函数将强制创建。。。

希望它能有所帮助!

您可以使用以下代码:

<?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."
";
    foreach ($_POST as $key => $value)
    $message .= "$key: $value
";

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

Hope it works. Thanks

append模式打开文件:

$fp = fopen($target_filename,  a );

您也可以简单地使用file_put_contents()

file_put_contents($target_filename, $message, FILE_APPEND);

接受此fopen()函数或您可以使用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
";

// 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追加。打开并写入文件末尾,如果不存在则创建新文件

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 

改为尝试

<?php
$file =  usersubmit_f456sd4f56sd4f.txt ;
// The new person to add to the file
 $person = "John Smith
";
// 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);
?>

文件输出内容

此函数与依次调用fopen()fwrite()[/code>和fclose()将数据写入文件相同

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...