Get the App
SLTechnology News&Howtos  ›  Development  › 

How to modify the data of a line in a file by php

Shulou Source: shulou.com Published: 2022-06-03 10:52:26 09月18日 Update

This article mainly introduces how php modifies the data of a certain line of the file, which is very detailed and has a certain reference value. Friends who are interested must read it!

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of modifying a line of data in a file by php

Open the specified file by reading and writing

Use the while () loop + fgets () function to control the number of rows

The fgets () function can read files line by line, one line at a time

When you specify the number of rows, use the fseek () and fwrite () functions to modify the data for that row.

Implementation code:

The original contents of the test.txt file:

Modified content:

Description:

The feof () function checks whether the end of the file (EOF) has been reached. TRUE is returned if there is an error or the file pointer reaches the end of the file (EOF), otherwise FALSE is returned.

The fseek () function locates in the open file. This function moves the file pointer forward or backward from the current position to a new location, which is measured in bytes from the header of the file. The function returns 0 on success and-1 on failure. Note that moving to the location after the end of the file (EOF) does not cause an error.

The fwrite () function can write a string to a file.

The above is all the contents of the article "how to modify the data of a certain line in a file by php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: File function line data location content end pointer article move success code value interest original character string byte boy buddy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno Docker Linux Redmi