dongwopu8210 2017-11-28 16:08
浏览 57
已采纳

如何在PHP中获取最后创建的目录?

Dynamically I am creating sub directory in a folder and I need to get the last created directory in a sting format, I have tried many ways but failed to get it. What i have tried is,

$path = "/path/to/my/dir";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
 }
}

But it works for only file. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douchun1961 2017-11-28 16:15
    关注

    Try something like this:

    $path = "/path/to/my/dir";
    $latest_mtime = 0;
    $latest_filename = '';
    if ($handle = opendir($path)) {
        $blacklist = array('.', '..');
        while (false !== ($file = readdir($handle))) {
            if (!in_array($file, $blacklist)) {
                if (is_dir($file) && filemtime($file) > $latest_mtime) {
                    $latest_mtime = filemtime($file);
                    $latest_filename = $file;
                }
            }
        }
        closedir($handle);
    }
    

    Also, you can change the condition of the blacklist for:

    if (!in_array($file, $blacklist) && substr($file, 0, 1) != ".") {
    

    to exclude hidden directories (in unix-like sistems).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计