search
HomeBackend DevelopmentPHP Problemphp continuously writes strings to a file

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool