PHP Development of file pointers, file lock

Source: Internet
Author: User
Tags flock rewind
(1) Rewind () function
This function sets the pointer of the file handle to the beginning of the file stream, with the following syntax:
BOOL Rewind (Resource handle)

(2) fseek () function
The Fseek () function implements the positioning of the file pointer with the following syntax:
int fseek (Resource handle,int offset[,int whence])
The handle parameter is the file to open
Offset to the pointer position or relative whence of the parameter, which can be a negative value.
Whence includes the following three types:
A,seek_set, the position equals the offset byte.
b, seek_cur, position equals current position plus offset offset.
C, Seek_end, where the position equals the end of the file plus offset offsets.
If you omit the whence parameter, the system defaults to Seek_set.

(3) feof () function
This function is used to determine if the file pointer is at the end of the file, with the following syntax:
BOOL Feof (Resource Hanlde)
Returns true if the file pointer is to the end of the file, otherwise false

(4) Ftell () function
The Ftell () function is used to return the position of the current pointer in the following syntax format:
int Ftell (resource handle)

The sample code is as follows:


    $filename="1.txt";if(Is_file ($filename)) {Echo"Total Files bytes:". FileSize ($filename)."
";$handle=fopen ($filename,"RB");Echo"The end of the pointer's initial position:". Ftell ($handle)."
"; Fseek$handle, -);//Move pointer positionEcho"The position of the pointer after using the fseek () function:". Ftell ($handle)."
";Echo"Output the contents after the current pointer:". fgets ($handle)."
";if(Feof ($handle)) {Echo"Current pointer points to end of file". Ftell ($handle)."
"; }Else{Echo"The current pointer does not have an ambition end:". Ftell ($handle)."
"; } Rewind ($handle);Echothe current pointer points to the position after using the rewind () function: ". Ftell ($handle)."
";Echo"Output after 25 bytes:". fgets ($handle, -)."
"; Fclose$handle);}Else{Echo"file does not exist";}?>

The results of the operation are as follows:

2, File lock

When you write to a file, you need to lock the file to prevent other users from modifying the file at the same time. In php, file locks are implemented using the flock () function. The syntax is as follows:
BOOL Flock (int handle,int operation)
Handle is an already opened file pointer, the operation parameter is as follows:

The example code for locking, writing, and unlocking closed files is as follows:


  
   $filename ="1.txt";$handle =fopen($filename"w");flock($handle, LOCK_EX);$str ="MMMMMMMMMMMMMMMMMMMMM";fwrite($handle$str);flock($handle, LOCK_UN);fclose($handle);readfile($filename);?>

The results of the operation are as follows:

The above describes the PHP development of the file pointer, file lock, including the content of the area, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.