PHP implements the linux command tail-f

Source: Internet
Author: User
Tags usleep
PHP implements the linux command tail-f

Today, I suddenly thought of a question that someone asked me before. how can I use PHP to implement the linux command tail-f? here I will analyze and implement it.

It's quite easy to think about this. check whether the file size has changed through a loop. if there is a change in the size of the output file, of course there will be a lot of details here, here is a detailed analysis.

If the initial file is too large or the content is changed too much

At this time, the output may not be clear, so I set a threshold value of 8192 here. when the content length exceeds this threshold, only the last 8192 bytes are output, in this way, you will not see the problem caused by a large area of refresh.

How to detect file size changes

This problem is the core of the program. whether the program can be successful or not depends on the performance. The implementation here is as follows:

  • Open the file handle $ fp. Note that the entire process of the file handle must be closed once, so you need to put it out of the loop.
  • Initialize the current file size file_size and file_size_new are both 0.
    • Update the file_size_new file size in the loop. Note that before getting the file size in php, run the clearstatcache () function to clear the file status cache. Otherwise, the file size may be deviated.
    • Calculate add_size = file_size_new-file_size to check whether the file size has changed. if there is any change, move the file pointer to the specified position, and then output the newly added content to update the file_size value to new_file_size.
    • Usleep (50000), sleep for 1/20 seconds.
Code implementation
#! /Usr/bin/env php
 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 code is implemented in the first line #! /Usr/bin/env php tells the executable file, and the executable file php is searched in the system PATH. this advantage is good portability.

11:28:51 improvement

I checked the official PHP Manual. the fseek function can be improved here. this function also accepts the third parameter, indicating the type of the offset pointer. the default value is SEEK_SET, which is offset from the beginning, you can also set it to SEEK_CUR, which indicates the offset from the current position. therefore, change it to fseek ($ fp, $ ignore_size, $ ignore_size );

The result is as follows:

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.