【问题标题】:php - Get the last modified dirphp - 获取最后修改的目录
【发布时间】:2018-04-04 17:08:00
【问题描述】:

有点卡在这个问题上,希望能得到一些帮助。我正在尝试从字符串中的路径获取最后修改的目录。我知道有一个名为“is_dir”的函数,我已经做了一些研究,但似乎没有任何工作。

对不起,我没有任何代码。

<?php
 $path = '../../images/'; 
 // echo out the last modified dir from inside the "images" folder
?>

例如: 上面的路径变量目前在“images”目录中有 5 个子文件夹。我想回显“sub5” - 这是最后修改的文件夹。

【问题讨论】:

  • 如果您单独找到答案,您可以将其作为答案单独发布(而不是将其编辑到问题本身中)吗?它更符合 Stack Overflow 作为问答网站的格式。

标签: php


【解决方案1】:

这也可以像其他答案一样工作。感谢大家的帮助!

<?php

    // get the last created/modified directory

    $path = "images/";

    $latest_ctime = 0;
    $latest_dir = '';    
    $d = dir($path);

    while (false !== ($entry = $d->read())) {
    $filepath = "{$path}/{$entry}";

    if(is_dir($filepath) && filectime($filepath) > $latest_ctime) {
      $latest_ctime = filectime($filepath);
      $latest_dir = $entry;
    }

    } //end loop

    echo $latest_dir;

    ?>

【讨论】:

    【解决方案2】:

    您可以使用scandir() 代替is_dir() 函数来做到这一点。

    这是一个例子。

    function GetFilesAndFolder($Directory) {
        /*Which file want to be escaped, Just add to this array*/
        $EscapedFiles = [
            '.',
            '..'
        ];
    
        $FilesAndFolders = [];
        /*Scan Files and Directory*/
        $FilesAndDirectoryList = scandir($Directory);
        foreach ($FilesAndDirectoryList as $SingleFile) {
            if (in_array($SingleFile, $EscapedFiles)){
                continue;
            }
            /*Store the Files with Modification Time to an Array*/
            $FilesAndFolders[$SingleFile] = filemtime($Directory . '/' . $SingleFile);
        }
        /*Sort the result as your needs*/
        arsort($FilesAndFolders);
        $FilesAndFolders = array_keys($FilesAndFolders);
    
        return ($FilesAndFolders) ? $FilesAndFolders : false;
    }
    
    $data = GetFilesAndFolder('../../images/');
    var_dump($data);
    

    在上面的示例中,最后修改的FilesFolders 将显示为升序。

    您还可以通过检查 is_dir() 函数来分离文件和文件夹,并将结果存储在 2 个不同的数组中,例如 $FilesArray=[]$FolderArray=[]

    关于filemtime()scandir()arsort()的详细信息

    【讨论】:

      【解决方案3】:

      您可以通过以下方式完成此操作:

      <?php
      
      // Get an array of all files in the current directory.
      // Edit to use whatever location you need
      $dir = scandir(__DIR__);
      
      $newest_file = null;
      $mdate = null;
      
      // Loop over files in directory and if it is a subdirectory and
      // its modified time is greater than $mdate, set that as the current
      // file.
      foreach ($dir as $file) {
          // Skip current directory and parent directory
          if ($file == '.' || $file == '..') {
              continue;
          }
          if (is_dir(__DIR__.'/'.$file)) {
              if (filemtime(__DIR__.'/'.$file) > $mdate) {
                  $newest_file = __DIR__.'/'.$file;
                  $mdate = filemtime(__DIR__.'/'.$file);
              }
          }
      }
      echo $newest_file;
      

      【讨论】:

        猜你喜欢
        • 2011-12-09
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-11
        • 2021-12-07
        • 1970-01-01
        相关资源
        最近更新 更多