: This article mainly introduces the php Development file pointer and file locking. if you are interested in the PHP Tutorial, please refer to it. (1) rewind () function
This function sets the pointer of the handle file to the beginning of the file stream. The syntax is as follows:
Bool rewind (resource handle)
(2) fseek () function
The fseek () function is used to locate the file pointer. The syntax is as follows:
Int fseek (resource handle, int offset [, int whence])
The handle parameter is the file to be opened.
Offset is the offset of the pointer position or relative to the whence parameter. it can be a negative value.
Whence includes the following three types:
A, SEEK_SET, with the position equal to the offset byte.
B, SEEK_CUR. the position is equal to the offset added to the current position.
C, SEEK_END. the position is equal to the offset added to the end of the file.
If the whence parameter is ignored, the default value is SEEK_SET.
(3) feof () function
This function is used to determine whether the file pointer is at the end of the file. the syntax format is as follows:
Bool feof (resource hanlde)
If the pointer to the end of the file is reached, true is returned; otherwise, false is returned.
(4) ftell () function
The ftell () function is used to return the position of the current pointer. the syntax format is as follows:
Int ftell (resource handle)
The sample code is as follows:
"; $ Handle = fopen ($ filename," rb "); echo" initial position tail of the pointer: ". ftell ($ handle )."
"; Fseek ($ handle, 25); // move the pointer position echo" position of the pointer after the fseek () function is used: ". ftell ($ handle )."
"; Echo" output content after the current pointer: ". fgets ($ handle )."
"; If (feof ($ handle) {echo" the current pointer points to the end of the file ". ftell ($ handle )."
";} Else {echo" the current pointer has no ambition to end: ". ftell ($ handle )."
";}Rewind ($ handle); echo" the current pointer points to the position after the rewind () function is used: ". ftell ($ handle )."
"; Echo" output 25 bytes: ". fgets ($ handle, 25 )."
"; Fclose ($ handle);} else {echo" file does not exist ";}?>
The running result is as follows:
2. file locking
When writing a file, you must first lock the file to prevent other users from simultaneously modifying the file. In php, the flock () function is used for file locking. Syntax:
Bool flock (int handle, int operation)
Handle is an opened file pointer. operation parameters are as follows:
The following is an example code of locking, writing, and unlocking and closing a file:
The running result is as follows: