Home > Article > Backend Development > PHP development file pointer, file locking
(1) rewind() function
This function sets the pointer of the file handle to the beginning of the file stream. The syntax is as follows:
bool rewind(resource handle)
(2)fseek() function
The fseek() function implements the positioning of 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 pointer position or the offset relative to whence parameters, which can be a negative value.
whence includes the following three types:
a, SEEK_SET, the position is equal to offset bytes.
b, SEEK_CUR, the position is equal to the current position plus offset offset.
c, SEEK_END, the position is equal to the end of the file plus the offset offset.
If the whence parameter is omitted, the system defaults to 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 file pointer reaches the end of the file, it returns true, otherwise it returns false
(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:
<code><span><span><?php</span><span>$filename</span> =<span>"1.txt"</span>; <span>if</span> (is_file(<span>$filename</span>)) { <span>echo</span><span>"文件总字节数:"</span>.filesize(<span>$filename</span>).<span>"<br>"</span>; <span>$handle</span> =fopen(<span>$filename</span>, <span>"rb"</span>); <span>echo</span><span>"指针的初始位置尾:"</span>.ftell(<span>$handle</span>).<span>"<br>"</span>; fseek(<span>$handle</span>, <span>25</span>); <span>//移动指针位置</span><span>echo</span><span>"使用fseek()函数后指针的位置:"</span>.ftell(<span>$handle</span>).<span>"<br>"</span>; <span>echo</span><span>"输出当前指针后面的内容:"</span>.fgets(<span>$handle</span>).<span>"<br>"</span>; <span>if</span> (feof(<span>$handle</span>)) { <span>echo</span><span>"当前指针指向文件末尾"</span>.ftell(<span>$handle</span>).<span>"<br>"</span>; } <span>else</span>{ <span>echo</span><span>"当前指针没有志向末尾:"</span>.ftell(<span>$handle</span>).<span>"<br>"</span>; } rewind(<span>$handle</span>); <span>echo</span><span>"使用rewind()函数后当前指针指向位置:"</span>.ftell(<span>$handle</span>).<span>"<br>"</span>; <span>echo</span><span>"输出25个字节后的内容:"</span>.fgets(<span>$handle</span>,<span>25</span>).<span>"<br>"</span>; fclose(<span>$handle</span>); }<span>else</span>{ <span>echo</span><span>"文件不存在"</span>; } <span>?></span></span></code>
The running results are as follows:
2, file lock
When a file is written, the file needs to be locked first to prevent other users from modifying it at the same time this file. In PHP, file locking is implemented using flock() function. The syntax is as follows:
bool flock(int handle,int operation)
handle is an open file pointer, and the operation parameters are as follows:
The sample code for locking, writing, unlocking and closing the file is as follows:
<code> <?php <span>$filename</span> =<span>"1.txt"</span>; <span>$handle</span> =<span>fopen</span>(<span>$filename</span>, <span>"w"</span>); flock(<span>$handle</span>, LOCK_EX); <span>$str</span> =<span>"MMMMMMMMMMMMMMMMMMMMM"</span>; <span>fwrite</span>(<span>$handle</span>, <span>$str</span>); flock(<span>$handle</span>, LOCK_UN); <span>fclose</span>(<span>$handle</span>); readfile(<span>$filename</span>); ?> </code>
The running results are as follows:
The above has introduced file pointers and file locking in PHP development, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.