PHP file writing, reading and replacing content_PHP tutorial
Perfectly implement PHP writing, reading, and replacing file content. Let me explain first, mainly used:
- fopen("File name.Extension", "Operation method")
- fwrite(read file, "written file");
- fclose(open object variable);
//写入文件 $str="This is a test from www.bkjia.comn"; // w表示以写入的方式打开文件,如果文件不存在,系统会自动建立 $file_pointer = fopen("aa.txt","a+"); fwrite($file_pointer,$str); fclose($file_pointer); //读取文件 $file_name="aa.txt"; $fp=fopen($file_name,'r'); while(!feof($fp)) { $buffer=fgets($fp,4096); //替换文件 $buffer = str_replace("bkjia","现代魔法",$buffer); $buffer = str_replace("This is a test","这是一个实例",$buffer); echo $buffer."<br />"; } fclose($fp);
fopen()
Thefopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened, and the second parameter specifies which mode to use to open the file.
Mode Description
- r Read only. Start at the beginning of the file.
- r+ read/write. Start at the beginning of the file.
- w Write only. Opens and clears the contents of the file; if the file does not exist, creates a new file.
- w+ read/write. Opens and clears the contents of the file; if the file does not exist, creates a new file.
- a append. Opens and writes to the end of a file, or creates a new file if it does not exist.
- a+ read/append. Maintain file contents by writing to the end of the file.
- x Write only. Create new file. Returns FALSE if the file exists.
- x+ read/write. Create new file. If the file already exists, returns FALSE and an error.
Note: If fopen() cannot open the specified file, it returns 0 (false).
feof()
Detect End-of-file
The feof() function detects whether the end of file (EOF) has been reached. The feof() function is useful when looping through data of unknown length. Note: In w, a and x modes, you cannot read open files!
if (feof($file)) echo "End of file";
fgets()
fgets() function is used to read a file line by line from a file. After calling this function, the file pointer moves to the next line.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Python, there are three ways to read .py files. The first method is to use the built-in function open(), such as withopen('example.py','r')asf:content=f.read(). The second method is to use the import statement, such as importexample. The third method is to use the exec() function, such as withopen('example.py','r')asf:code=f.read()exec(code).

How to optimize file writing multi-threaded concurrency performance in Java development. In large-scale data processing scenarios, file read and write operations are inevitable, and in the case of multi-thread concurrency, how to optimize file writing performance becomes particularly important. important. This article will introduce some methods to optimize multi-threaded concurrent performance of file writing in Java development. Reasonable use of buffers During the file writing process, using buffers can greatly improve writing performance. Java provides a variety of buffer implementations, such as ByteBuffer, CharBuffe

PHP file processing skills: Efficiently read and write files In the process of web development, we often need to read and write files, such as configuration files, log files, uploaded files, etc. However, file operations may affect system performance and efficiency. Therefore, we need to master some efficient file processing skills to improve system performance and user experience. This article will introduce some file processing techniques in PHP, as well as optimization methods for reading and writing files, and provide corresponding code examples. Efficiently read files 1.1 using fil

Golang file reading operation: Tips for quickly reading large files, specific code examples are required In Golang programming, file reading is a very common operation. But when large files need to be read, it is usually a time- and resource-consuming operation. Therefore, how to read large files quickly is a topic worth discussing. This article will introduce how to use Golang's features and some techniques to quickly read large files, and provide specific code examples. Using bufio to read files in Golang, file reading

Golang is a programming language known for its efficiency and speed, but when it comes to file reading, you will fall into a performance bottleneck if you are not careful. This article will discuss the optimization of file reading in Golang, introduce tips that can improve program performance, and come with specific code examples. Using buffers In Golang, when reading a file, a system call of the operating system is executed every time a byte is read, which is an extremely time-consuming operation. Therefore, it is recommended to use buffer technology to improve file reading efficiency. A buffer refers to a pre-allocated memory

PHP is a server-side programming language that developers can use to develop various types of web applications. When developing web applications, file operations may be a frequently used function. In this article, we will provide an in-depth introduction to file manipulation guidelines in PHP. 1. Create a file Creating a file in PHP is very simple. You only need to use the fopen function to open the file handle, then use the fwrite function to write data, and use the fclose function to close the file handle. Example: $myF

In C++, you can use the ofstream class to write files, open the file through the open() method, and use

The fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure. Syntax fread(file_pointer,length) Parameter file_pointer−The file system pointer resource created using fopen(). Required. length−Maximum number of bytes to read. Required. Return Value If successful, the fread() function returns the read string. On failure, returns FALSE. Suppose we have a file called "one.txt" where
