PHP implements the linux command tail -f
Today I suddenly thought of a question someone asked me before, how to implement the linux command tail -f through PHP. Let’s analyze and implement it here.
This is quite simple when you think about it. It detects the file through a loop to see if the size of the file has changed. If there is a change, output the changed part of the file. Of course, there will be a lot of details in it. Here is a detailed analysis.
If the initial file is too large or the content has been changed too much
A lot of content may be output at once and may not be visible clearly, so I set a threshold of 8192 here. When the content length exceeds this threshold, only the last 8192 will be output. Bytes, so that there will be no problem of unclear visibility caused by large-area refresh.
How to detect changes in file size
This question is the core of this program. Whether it can be successful or not depends on this part of the performance. My implementation here is as follows:
Open the file handle $fp. It should be noted here that the file handle here only needs to be opened once and closed once in the whole process, so it must be placed outside the loop.
Initialize the current file size file_size and file_size_new to 0.
The file_size_new file size is updated in the loop. It should be noted here that before obtaining the file size in PHP, the function clearstatcache() must be run to clear the file status cache, otherwise there may be deviation in obtaining the file size.
Calculate add_size = file_size_new - file_size to see if the file size changes. If there is a change, move the file pointer to the specified position, then output the newly added content, and update the file_size value to new_file_size.
usleep(50000), sleep for 1/20 seconds.
Code implementation
#!/usr/bin/env php <?php if(2 != count($argv)){ fwrite( STDERR, "调用格式错误!使用格式 ./tail filename".PHP_EOL ); return 1; } $file_name = $argv[1]; define("MAX_SHOW", 8192); $file_size = 0; $file_size_new = 0; $add_size = 0; $ignore_size = 0; $fp = fopen($file_name, "r"); while(1){ clearstatcache(); $file_size_new = filesize($file_name); $add_size = $file_size_new - $file_size; if($add_size > 0){ if($add_size > MAX_SHOW){ $ignore_size = $add_size - MAX_SHOW; $add_size = MAX_SHOW; fseek($fp, $file_size + $ignore_size); } fwrite( STDOUT, fread($fp, $add_size) ); $file_size = $file_size_new; } usleep(50000); } fclose($fp);
The first line of code implementation here is #!/usr/bin/env php, which tells the executable file that the executable file php is to be found in the system PATH. The advantage of this is that it has good portability.
2016-02-22 11:28:51Improvement
I checked the PHP official manual and found that the fseek function can be improved here. This function also accepts a third parameter, indicating the type of offset pointer. The default is SEEK_SET , offset from the beginning, can also be set to SEEK_CUR, which means offset from the current position, so here it is changed to fseek($fp, $ignore_size, $ignore_size);
Here are the results