需要用PHP文件开头写 [英] Need to write at beginning of file with PHP
本文介绍了需要用PHP文件开头写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做这个节目,我试图找出如何将数据写入文件,而不是结束的开始。 A/追加只写到底,我怎么可以把它写的开始?因为R +确实,但覆盖previous数据。
$ =数据B的fopen('database.txt,R +);
下面是我的整个文件:
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01过渡// ENhttp://www.w3.org/TR/html4/loose。 DTD>
< HTML和GT;
< HEAD>
<标题> Facebook的V0.1< /标题>
<风格类型=文/ CSS>
#bod {
保证金:0汽车;
宽度:800像素;
边界:2px的固体黑色;
}
< /风格>
< /头> <身体GT;
< DIV ID =BOD>
< PHP
$ FNAME = $ _REQUEST ['FNAME'];
$ LNAME = $ _REQUEST ['L-NAME'];
$评论= $ _REQUEST ['评论'];
$数据B = $ _REQUEST ['数据B'];
$ GFILE = $ _REQUEST ['GFILE']; 打印<<<形式
<表边框=2的风格=保证金:0汽车;>
&所述; TD>
<形式方法=邮报行动=>
名字 :
<输入类型=文本
NAME =FNAME
值=>
< BR> 姓 :
<输入类型=文本
NAME =LNAME
值=>
< BR> 评论:
<输入类型=文本
NAME =评论
值=>
< BR>
<输入类型=提交值=提交>
< /表及GT;
< / TD>
< /表>
形成;
如果((空($ FNAME))及!及(空($ LNAME))及!&安培;!(空($评论))){
$形式=<<<来 <表边框='2'WIDTH =300像素的风格=保证金:0汽车;>
&所述; TR>
&所述; TD>
<跨度风格=颜色:蓝色;字体重量:大胆;>
$ FNAME LNAME $:
< / SPAN> $评论
< / TD>
< / TR>
< /表>
来; $数据B =的fopen('database.txt,R +);
的fputs($数据B,$形式);
FCLOSE($数据B); }否则如果((空($ FNAME))及及(空($ lname的))及及(空($评语))){
打印请输入数据;
} // 茶几 $数据B =的fopen('database.txt,R); 而(!的feof($数据B)){
$ GFILE =与fgets($数据B);
打印$ GFILE
} //而结束
?>
< / DIV>
< /身体GT;
< / HTML>
解决方案
快速和肮脏的:
< PHP
$ file_data =东西你想添加\\ n;
$ file_data =的file_get_contents('database.txt')。
的file_put_contents('database.txt',$ file_data);
?>
I'm making this program and I'm trying to find out how to write data to the beginning of a file rather than the end. "a"/append only writes to the end, how can I make it write to the beginning? Because "r+" does it but overwrites the previous data.
$datab = fopen('database.txt', "r+");
Here is my whole file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Facebook v0.1</title>
<style type="text/css">
#bod{
margin:0 auto;
width:800px;
border:solid 2px black;
}
</style>
</head>
<body>
<div id="bod">
<?php
$fname = $_REQUEST['fname'];
$lname = $_REQUEST['lname'];
$comment = $_REQUEST['comment'];
$datab = $_REQUEST['datab'];
$gfile = $_REQUEST['gfile'];
print <<<form
<table border="2" style="margin:0 auto;">
<td>
<form method="post" action="">
First Name :
<input type ="text"
name="fname"
value="">
<br>
Last Name :
<input type ="text"
name="lname"
value="">
<br>
Comment :
<input type ="text"
name="comment"
value="">
<br>
<input type ="submit" value="Submit">
</form>
</td>
</table>
form;
if((!empty($fname)) && (!empty($lname)) && (!empty($comment))){
$form = <<<come
<table border='2' width='300px' style="margin:0 auto;">
<tr>
<td>
<span style="color:blue; font-weight:bold;">
$fname $lname :
</span>
$comment
</td>
</tr>
</table>
come;
$datab = fopen('database.txt', "r+");
fputs($datab, $form);
fclose($datab);
}else if((empty($fname)) && (empty($lname)) && (empty($comment))){
print" please input data";
} // end table
$datab = fopen('database.txt', "r");
while (!feof($datab)){
$gfile = fgets($datab);
print "$gfile";
}// end of while
?>
</div>
</body>
</html>
解决方案
The quick and dirty:
<?php
$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('database.txt');
file_put_contents('database.txt', $file_data);
?>
这篇关于需要用PHP文件开头写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文