Home > Daily Programming > PHP Knowledge > How to continuously write content to the header of a file in PHP? (Pictures + Videos)

How to continuously write content to the header of a file in PHP? (Pictures + Videos)

藏色散人
Release: 2019-12-20 17:58:13
Original
6935 people have browsed it

This article mainly introduces to you the specific implementation method of PHP continuously writing content to the file header.

During our PHP interview process, basic knowledge about PHP operating files is also one of the common test points.

Below we combine a simple example to introduce how PHP implements continuous writing of content to the file header.

Example: We need to continuously write content to the header of a file called "hello.txt", that is, automatically write content to the header every time the page is refreshed.
The code example is as follows:

<?php
/**
 * 不断的向文件头部写入内容
 */
$fileName = &#39;hello.txt&#39;;
if (!is_file($fileName)) {
    touch(&#39;hello.txt&#39;);
    $file = fopen($fileName, &#39;rb+&#39;);
    fwrite($file, &#39;hello world&#39;);
    fclose($file);
    return ;
} else {
    $file = fopen($fileName, &#39;r&#39;);
    $content = fread($file, filesize($fileName));
    $hello = &#39;hello world123&#39; . PHP_EOL . $content;
    fclose($file);
    $file = fopen($fileName, &#39;w&#39;);
    fwrite($file, $hello);
    fclose($file);
}
Copy after login

In this code, we mainly write an if judgment statement. First, judge whether the file "hello.txt" exists. If it does not exist, pass it. touch function to create and write the contents of 'hello world' to the file through the fwrite function.

Then we continue to write content to the file header. Here we need to write 'hello world123' in the file content header.

Finally, we can refresh it 3 times through the browser access, and the results can be obtained as follows:

How to continuously write content to the header of a file in PHP? (Pictures + Videos)

Introduction to related functions:

fopen — Open a file or URL. (rb opens a binary file for reading and writing, allowing only reading and writing of data. r means opening a read-only file, and the file must exist.)

fclose —Close an open file pointer.

touch() Function sets the access and modification time of the specified file. If the file does not exist, it will be created.

fread() Function reads a file (safe for binary files).

fwrite() Function writes to a file (safe for binary files).

PHP_EOL: PHP’s line break character, indicating text line break.

So about some basic operations of PHP files, we have also introduced it to you in detail in previous articles. Friends who need it can choose to refer to and learn:

How does PHP add files to the end of the file? Additional content?

How does PHP write data to a specified file?

How to read file content in PHP?

What are the simple ways to upload multiple files in PHP?

Detailed explanation of how PHP obtains the file size

This article is about the specific implementation method of PHP continuously writing content to the file header. It is simple. Easy to understand, I hope it will be helpful to friends who need it!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to continuously write content to the header of a file in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template