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 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架