Table of Contents
fopen()
feof()
fgets()
Home Backend Development PHP Tutorial PHP file writing, reading and replacing content_PHP tutorial

PHP file writing, reading and replacing content_PHP tutorial

Jul 13, 2016 am 10:33 AM
file writing file reading

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);
Copy after login

fopen()

The

fopen() 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";
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752504.htmlTechArticlePerfectly implements PHP writing, reading, and replacing file content. Let me explain first, mainly used: fopen("file name.extension", "operation mode") fwrite(read file, "written file"); fclos...
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read .py files correctly in Python? How to read .py files correctly in Python? Apr 03, 2024 pm 04:21 PM

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).

What are the methods to optimize multi-threaded performance of Java file writing? What are the methods to optimize multi-threaded performance of Java file writing? Jul 01, 2023 am 10:05 AM

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 tips: read and write files efficiently PHP file processing tips: read and write files efficiently Sep 06, 2023 am 11:36 AM

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 operations: tips for reading large files quickly Golang file reading operations: tips for reading large files quickly Jan 19, 2024 am 08:33 AM

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 file reading optimization: tips to improve program performance Golang file reading optimization: tips to improve program performance Jan 19, 2024 am 08:59 AM

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

Guide to File Operations in PHP Guide to File Operations in PHP May 22, 2023 am 08:40 AM

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

How to write to a file using C++? How to write to a file using C++? Jun 04, 2024 pm 07:01 PM

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

fread() function in PHP fread() function in PHP Sep 07, 2023 pm 11:57 PM

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

See all articles