Home  >  Article  >  Backend Development  >  php continuously writes strings to a file

php continuously writes strings to a file

藏色散人
藏色散人Original
2019-10-15 09:27:113371browse

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,&#39;r&#39;指的是用只读的方式打开,指针(光标)放在文件头,
$fp=fopen(&#39;hello.txt&#39;,&#39;r&#39;);
//定义要写入的字符串,"\n"是换行的意思,如果没有换行,可以吧"\n"替换成"\r\n";
$str=&#39;hello!&#39;."\n";
//filesize(&#39;hello.txt&#39;),获得次文件的大小字节,那么这一行的意思是读取hello.txt的内容,
//$str.=fread($fp,filesize(&#39;hello.txt&#39;)); 和$str = $str.fread($fp,filesize(&#39;hello.txt&#39;));是等同的
$str.=fread($fp,filesize(&#39;hello.txt&#39;));
//关闭$fp句柄,就是把内存占用和句柄释放掉
fclose($fp);
//不解释
$fp1=fopen(&#39;hello.txt&#39;,&#39;w&#39;);
//用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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn