Home > Article > Backend Development > php continuously writes strings to a file
php Continuously writes a string in a file
php /*Continuously writes a string in the header of the file hello.txt Write a line of "hello world" string, the code is required to be complete*/
The PHP code is as follows:
<?php //fopen,指定文件位置和模式,hello.txt指的就是当前目录下的hello.txt文件,如果在当前目录下的test文件夹下,就应该是test/hello.text,'r'指的是用只读的方式打开,指针(光标)放在文件头, $fp=fopen('hello.txt','r'); //定义要写入的字符串,"\n"是换行的意思,如果没有换行,可以吧"\n"替换成"\r\n"; $str='hello!'."\n"; //filesize('hello.txt'),获得次文件的大小字节,那么这一行的意思是读取hello.txt的内容, //$str.=fread($fp,filesize('hello.txt')); 和$str = $str.fread($fp,filesize('hello.txt'));是等同的 $str.=fread($fp,filesize('hello.txt')); //关闭$fp句柄,就是把内存占用和句柄释放掉 fclose($fp); //不解释 $fp1=fopen('hello.txt','w'); //用fwrite向hello.txt写入$str,结束是我建议再次调用fclose,避免内存占用 fwrite($fp1,$str); ?>
For more PHP-related knowledge, please visit PHP Chinese website!
The above is the detailed content of php continuously writes strings to a file. For more information, please follow other related articles on the PHP Chinese website!