php-获取最后修改的目录

php-获取最后修改的目录,php,Php,有点困在这上面,希望得到一些帮助。我试图从字符串中的路径获取最后修改的目录。我知道有一个名为“”的函数,我做了一些研究,但似乎什么都没做 对不起,我没有密码 <?php $path = '../../images/'; // echo out the last modified dir from inside the "images" folder ?> 例如:上面的path变量当前在“images”目录中有5个子文件夹。我想呼出“sub5”-这是最后修改的文件夹 您可以

有点困在这上面,希望得到一些帮助。我试图从字符串中的路径获取最后修改的目录。我知道有一个名为“”的函数,我做了一些研究,但似乎什么都没做

对不起,我没有密码

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

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

您可以使用
scandir()
而不是
is\u 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);
从上面的示例中,上次修改的
文件
文件夹
将显示为升序

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


关于

的详细信息这里有一种方法可以实现这一点:

<?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;

这将与其他答案一样有效。谢谢大家的帮助

<?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;

    ?>

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