递归浏览所有服务器目录并使用php列出最新创建的文件 [英] Recursive browse all server directories and list newest created files with php

查看:46
本文介绍了递归浏览所有服务器目录并使用php列出最新创建的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常常见的问题,仍然没有找到正确的解决方案.我需要每天早上运行cron job来启动php脚本,以列出夜间在Web服务器上创建的所有新文件.这对于查看访问者在夜间上传的内容非常有用,当然,这些文件可能是有害文件,可能会损害其他访问者计算机.到目前为止,我有这个:

very common question and still did not find the right solution for this. I need to run cron job to start php script each morning to list all new files created on the web server during the night. This is very usefull to see what visitors have uploaded during the night and ofcourse very ofter those files could be harmful files that will hurt other visitors computers. So far I have this:

$dir = "../root/";         
$pattern = '\.*$'; // check only file with these ext.              
$newstamp = 0;                
$newname = "";    
if ($handle = opendir($dir)) {                   
       while (false !== ($fname = readdir($handle)))  {                
         // Eliminate current directory, parent directory                
         if (ereg('^\.{1,2}$',$fname)) continue;                
         // Eliminate other pages not in pattern                
         if (! ereg($pattern,$fname)) continue;                
         $timedat = filemtime("$dir/$fname");                
         if ($timedat > $newstamp) {    
            $newstamp = $timedat;    
            $newname = $fname;    
          }    
         }    
        }    
closedir ($handle);    

// $newstamp is the time for the latest file    
// $newname is the name of the latest file    
// print last mod.file - format date as you like                
print $newname . " - " . date( "Y/m/d", $newstamp);    

这将打印最新文件,但仅在一个目录root/中进行打印,并且不检查例如root/folder/等.如何重新执行该操作?如果我在根目录/文件夹中添加新文件,脚本将显示带date的文件夹,但不会显示在根目录/文件夹中创建了哪个文件..希望您明白我的意思,谢谢

this prints the newest file but only in one directory root/ and doesnt check for example root/folder/ and etc.. How to do it recusrsivly ? If I add a new file within root/folder the script will show me folder with date , but would not show which file in root/folder was created.. I hope you understand what I mean, thanks

推荐答案

满足您需求的快速脚本(在Windows 7上使用cygwin和ubuntu 12.10在 PHP 5.3.10 上进行了测试)

Quick script that does what you want (tested under windows 7 with cygwin and under ubuntu 12.10 with PHP 5.3.10)

<?php
$path = $argv[1];
$since = strtotime('-10 second'); // use this for previous day: '-1 day'

$ite = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
foreach ( new RecursiveIteratorIterator($ite) as $filename => $object ) {
    if (filemtime($filename) > $since) {
      echo "$filename recently created\n";
    }
}


我的快速测试:


My quick test:

$> mkdir -p d1/d2
$> touch d1/d2/foo
$> php test.php .
./d1/d2/foo recently created
$> php test.php . # 10secs later 
$>

这篇关于递归浏览所有服务器目录并使用php列出最新创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆