php 写文本文件php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14575253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write text file php
提问by furyfish
Im using php.
我使用 php。
I want to write a php page to get parameters from another page and write to a file text. And:
我想编写一个php页面来从另一个页面获取参数并写入文件文本。和:
If already have file text, it write to a new line
Each day create one file text
如果已经有文件文本,则写入新行
每天创建一个文件文本
Example:
例子:
register.php
注册.php
<body>
<form action='process.php' method='GET'>
Name: <input type='text' name='name'/>
<br/>
Age: <input type='text' name='age'/>
<br/>
<input type='submit' value='SUBMIT'/>
</form>
</body>
process.php
进程.php
$name = $_GET['name'];
$age = $_GET['age'];
$file_handle = fopen("testFile.txt", "w");
$file_contents = "name:" . $name . "age:" . $age;
fwrite($file_handle, $file_contents);
fclose($file_handle);
print "file created and written to";
How can I do this?
我怎样才能做到这一点?
采纳答案by furyfish
Thanks. I resolved this problem with this http://www.redips.net/php/write-to-log-file/
谢谢。我用这个http://www.redips.net/php/write-to-log-file/解决了这个问题
回答by sgarbesi
Use the file_get_contents() and file_put_contents() functions.
使用 file_get_contents() 和 file_put_contents() 函数。
Example:
例子:
$file = 'output.txt';
$buffer = 'my new line here';
if (file_exists($file)) {
$buffer = file_get_contents($file) . "\n" . $buffer;
}
$success = file_put_contents($file, $buffer);
http://php.net/manual/en/function.file-put-contents.php
http://php.net/manual/en/function.file-put-contents.php
回答by Devang Rathod
If you want to create new file for everyday , you can create txt file with current date date('d-m-Y').".txt". so you can easily identify file by date.
如果您想为每天创建新文件,您可以创建具有当前日期的 txt 文件date('d-m-Y').".txt"。这样您就可以轻松地按日期识别文件。
try below code, i have made some change in code and test it.
尝试下面的代码,我对代码进行了一些更改并对其进行了测试。
<?php
$dateFile = date('d-m-Y').".txt";
$dataString = "name:" . $name . "age:" . $age."\n";
$fWrite = fopen($dateFile,"a");
$wrote = fwrite($fWrite, $dataString);
fclose($fWrite);
print "file created and written to";
?>
If file already created so you can store new record in new line by "\n", \n must be in "" quatoed.
如果文件已经创建,因此您可以通过 将新记录存储在新行中"\n",\n 必须在 "" quatoed 中。
回答by sachleen
From the fopen docs:
从fopen 文档:
write:
写:
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.
w 只允许写;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。
append:
附加:
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.
a 只供写作;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。
So you want to append, not write.
所以你想追加,而不是写。
If the file doesn't exist, it'll try to create it. So just give the file a unique name that changes with the day. See datedocs for examples.
如果文件不存在,它会尝试创建它。因此,只需为文件指定一个随时间变化的唯一名称即可。有关示例,请参阅日期文档。
$filename = date("m.d.y"); // 03.10.01
回答by Hitesh
<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "Jane Doe\n".PHP_EOL;;
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
Above is simple example to do it in php
以上是在 php 中执行此操作的简单示例
回答by mitim
I haven't written a lot of php, but if you replace "w" with "w+" in fopen() it'll open the file and place the pointer at the end of it if the file already exists, making any fwrite() calls append to the file.
我没有写很多 php,但是如果你在 fopen() 中用“w+”替换“w”,它会打开文件并将指针放在它的末尾(如果文件已经存在),使任何 fwrite( ) 调用附加到文件。
$file_handle = fopen("testFile.txt", "w+");
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fopen.php
edit:saw sachleen's answer and yes, you could combine the date (down to the day) with the file name. So that on a new day a new file would be created for that day and if the file exists for that day, it will be appended to.
编辑:看到 sachleen 的回答,是的,您可以将日期(精确到当天)与文件名结合起来。因此,在新的一天,将为该天创建一个新文件,如果该文件在当天存在,则将其附加到。

