【问题标题】:How to get PHP files and their path changed after a date如何在某个日期后更改 PHP 文件及其路径
【发布时间】:2019-01-26 07:40:21
【问题描述】:

我想使用 PHP 获取在某个日期之后(在目录和子目录中)更新的文件的所有文件名(和路径)。

与 20.08.2017 之后更新的所有文件一样,

下面的代码只提供目录中的文件,我还需要路径,

$dir = "opendir(".")";
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
    if ( substr($file,-4) == ".php" ) 
    {
    if (filemtime($file) >= $yesdate)
    {
        echo $file;
    }
}

}

谢谢

【问题讨论】:

标签: php


【解决方案1】:

如果您使用相对路径,例如. 或符号链接后的路径,您可以通过函数realpath 获取真实路径:

$actualDirectory = realpath(".");
$dir = opendir($actualDirectory);
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
    if ( substr($file,-4) == ".php" && filemtime($file) >= $yesdate)
    {
        echo $actualDirectory."/".$file;
    }
}

【讨论】:

    【解决方案2】:

    你必须这样进行

    function get_updated_files($date, $directory,$file_extension='php',$sameday=false, $result = array())
    {
        if(!$sameday){
            /* really worth because actually 
               your code will  return true for your given day and this maybe
               is not the goal you are trying to achieve...*/
            $date = strtotime($date)+86399;
        }else{
            $date=strtotime($date);
        }
        $directory = realpath($directory);
        $directory_content = glob($directory.'\\*');
        foreach($directory_content as $item) {
            if(is_dir($item)) {
                $result=get_updated_files($date, $item,$file_extension,$sameday,$result);
            } elseif($date < filemtime($item)&& pathinfo($item, PATHINFO_EXTENSION)===$file_extension) {
                $result[] = $item;
            }
        }
    
    
    
    
       return $result;
    }
    
    $result = get_updated_files('2017-08-20', '.');
    

    【讨论】:

    • @apokrytos 我把这篇文章写得很通用。你可以在需要时获得所有需要的扩展,但值得一提的是,当文件与你给定的日期相同时,它会注意不返回 true。 ..
    【解决方案3】:

    如果要扫描所有子目录直到树的末尾,则需要使用递归函数。

    function get_updated_files($date, $directory, $result = array())
    {
        $directory = realpath($directory);
        $directory_content = glob($directory.'/*');
        foreach($directory_content as $item) {
        if(is_dir($item)) {
            $result = get_updated_files($date, $item, $result);
        } elseif(strtotime($date) < filemtime($item) && pathinfo($item, PATHINFO_EXTENSION) == 'php') {
            $result[] = $item;
        }
        }
        return $result;
    }
    
    $result = get_updated_files('2017-08-20', '.');
    

    【讨论】:

    • $directory_content = glob($directory.'/*.php');
    • @Gabor 如果你设置 *.php 你将无法访问子目录。
    • @infugin 可能的解决方案之一是使用 pathinfo 获取文件扩展名。我已经在 elseif 子句中编辑了脚本。
    • @SamuilBanti ,glob() 函数导致内存耗尽错误,268435456 字节耗尽(尝试分配 32 字节)
    • @infugin 你需要两件事。 1.将函数的第二个参数设置为你需要的路径:get_updated_files('2017-08-20', '/home/dev/public_html'); 2.确保执行脚本的用户具有从目录读取所需的权限。
    【解决方案4】:

    指定日期:

    $dir = opendir(dirname(__FILE__));
    clearstatcache();
    $dday = "20.08.2017.";
    $yesdate = strtotime($dday);
    while(false != ($file = readdir($dir))) {
       if ( substr($file,-4) == ".php" ) {
          if (filemtime($file) >= $yesdate) {
             echo $file;
          }
       }
    }
    

    或者今天 - 1 天:

    $dday = date("d.m.Y", time());
    $yesdate = strtotime($dday) - 86400;
    

    【讨论】:

    • 这不是提供文件路径,如果该文件在 2 个文件夹中,如何获取根文件?
    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2016-08-31
    相关资源
    最近更新 更多