如何持续监视目录中是否存在具有特定文件扩展名的文件


How to continuously watch a directory for the presence of files with a certain file-extension?

例如,我想持续监视目录中是否存在.xml文件。

一旦在该目录中找到.xml文件,程序就应该开始对该文件进行处理,例如读取文件中的数据,提取有用的数据和进一步的操作。

解决方案我试图使用:

我尝试使用INFINITE while循环连续监视目录,并使用glob()来检查.xml扩展名文件的存在。glob()返回一个包含找到的所有文件路径的数组。

那么在无限while循环中,我检查glob()返回的数组是否为NON-EMPTY。如果是,那么我在glob()返回的数组中的每个路径上读取文件,并对文件进行适当的处理。

问题是当我运行它时,我得到

Fatal error: Maximum execution time of 30 seconds exceeded in C:'xampp'htdocs'Alpha'index.php on line 9

我认为这是由于无限while循环。

问题:

  1. 我解决问题的方法是好方法吗?

  2. 我该怎么做才能绕过上面的错误?

作为使用SSE实现监视目录的既定意图的示例

执行目录扫描的PHP脚本—在本例中,它是一个简单的glob,但可以使用recursiveIterator等更复杂的

<?php
    /*
        sse.php
    */
    set_time_limit( 0 );
    ini_set('auto_detect_line_endings', 1);
    ini_set('max_execution_time', '0');
    ob_end_clean();
    /* -- set headers -- */
    header('Content-Type: text/event-stream'); /* !important! */
    header('Cache-Control: no-cache');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Methods: GET');
    header('Access-Control-Expose-Headers: X-Events');  
    /* -- utility function to send formatted sse message -- */
    if( !function_exists('sse_message') ){
        function sse_message( $evtname='dirmon', $data=null, $retry=1000 ){
            if( !is_null( $data ) ){
                echo "event:".$evtname."'r'n";
                echo "retry:".$retry."'r'n";
                echo "data:" . json_encode( $data, JSON_FORCE_OBJECT|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS );
                echo "'r'n'r'n";
            }
        }
    }
    /* How often messages will be pushed to the client app */
    $sleep = 5;
    /* The directory to monitor */
    $dir = 'c:/temp/';
    /* The name of the event to monitor in js client app */
    $evt = 'dirmon';

    while( true ){
        if( connection_status() != CONNECTION_NORMAL or connection_aborted() ) {
            break;
        }
        /* Methods here to scan directory and produce data to send */
        $files=glob( $dir . '*.xml' );
        $count = count( $files );
        $payload=array(
            'dir'   =>  $dir,
            'files' =>  $count
        );
        /* -- prepare and send sse message -- */
        sse_message( $evt, $payload );
        /* -- Send output -- */
        if( @ob_get_level() > 0 ) for( $i=0; $i < @ob_get_level(); $i++ ) @ob_flush();
        @flush();
        /* wait */
        sleep( $sleep );
    }
    if( @ob_get_level() > 0 ) {
        for( $i=0; $i < @ob_get_level(); $i++ ) @ob_flush();
        @ob_end_clean();
    }
?>

客户端页面

<!doctype html>
<html>
    <head>
        <title>SSE</title>
        <script type='text/javascript'>
            var count=0;
            function bindEvtSource(){
                var evt='dirmon';
                var url='/sse.php';
                if ( !!window.EventSource ) {
                    var evtSource = new EventSource( url );
                    evtSource.addEventListener( 'open', function(e){
                        console.log(e.type);
                    },false);
                    evtSource.addEventListener( 'error', function(e){
                        console.error('%o %s',e,e.type);
                    },false);
                    evtSource.addEventListener( evt, function(e){
                        var json=JSON.parse(e.data);
                        var files=json.files;
                        if( files!=count ) {
                            count=files;
                            alert('contents changed - process changes! Send Ajax Request? etc etc');
                        }
                        document.getElementById('dirdata').innerHTML=e.data;
                    },false);

                } else {
                    alert('Server Sent Events are not supported in this browser');
                }
            }
            document.addEventListener( 'DOMContentLoaded', bindEvtSource, false );
        </script>
    </head>
    <body>
        <div id='dirdata'></div>
    </body>
</html>

你可以使用响应,然后通过在js中启动一个函数来执行进一步的操作-即:将Ajax请求发送到一个脚本,该脚本将执行你需要的任何处理(服务器发送的事件是单向的,你可以发送/接收-只能接收!)