PHP file reading and writing operations learning
Read file:
Write file:
and append it to the end of the file:
The above is just a brief introduction, below we will Discuss something deeper. Sometimes multiple people write (most commonly on websites with large traffic), which results in useless data being written to the file, for example: info.file file content is as follows -> |1|Mukul|15|Male|India (n) |2|Linus|31|Male|Finland (n) Now two people register at the same time, causing file corruption-> info.file -> |1|Mukul|15|Male|India |2|Linus|31|Male|Finland |3|Rob|27|Male|USA| Bill|29|Male|USA In the above example, when PHP writes Rob's information to the file, Bill happens to start writing. At this time, it happens that the 'n' of Rob's record needs to be written, causing the file to be damaged. We certainly don’t want this to happen, so let’s look at file locking:
In the above example, if the two files read.php and read2.php both need to access the file, then they can both read it, but when a program needs to write, it must wait until the read operation is completed. , the file is released.
Although the "w" mode is used to overwrite files, I don't think it is applicable.
Hmmm..., Appending data is a little different from other operations, it is FSEEK! It is always a good habit to make sure the file pointer is at the end of the file. If it is under Windows system, the above file needs to be preceded by ''. FLOCK Miscellaneous Talk: Flock() only locks the file after it is opened. In the above column, the file is locked after it is opened. Now the content of the file is only the content at that time, and does not reflect the results of other program operations. Therefore, fseek should be used not only for file append operations, but also for read operations. (The translation here may not be exact, but I think I get the idea). About the pattern: 'r' - Open read-only, with the file pointer at the beginning of the file 'r+' - Open for reading and writing, place the file pointer at the beginning of the file 'w' - open for writing only, the file pointer is placed at the beginning of the file, the file is cut to 0 bytes, if the file does not exist, try to create the file 'w+' - Open for reading and writing, the file pointer is placed at the beginning of the file, the file size is cut to 0 bytes, if the file does not exist, try to create the file 'a' - Open for writing only, the file pointer is placed at the end of the file, if the file does not exist, try to create the file 'a+' - open for reading and writing, the file pointer is placed at the end of the file, if the file does not exist, try to create the file By the way, the code to create the file directory
The above article is for beginners’ reference, and experts can pass it by. |

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
