简体   繁体   English

回声目录php中的最新文件夹?

[英]echo most recent folder in directory php?

I've tried this code, but it displays ALL folders in a given directory. 我已经尝试过此代码,但是它将显示给定目录中的所有文件夹。

<?php $dir = './folder';$ffs = scandir($dir); foreach($ffs as $ff){ if($ff != '.' && $ff != '..'){ if(strpos($ff, '.')) {} else { $ff = ucfirst($ff); echo "<a href='../subjects/" . strtolower($ff) . "'>".$ff.'</a><br>'; } } } echo '</ol>'; }?>

How would I use PHP to echo the most recent FOLDER in a directory? 如何使用PHP回显目录中的最新文件夹?

just use the last entry of the filelist. 只需使用文件列表的最后一个条目即可。 You dont need to iterate over it 您不需要对其进行迭代

<?php 
    $dir = './folder';
    $ffs = scandir($dir);
    if (is_array($ffs)) {
        $name_of_last_file = $ffs[count($ffs) - 1];
        if ($name_of_last_file != ".." && $name_of_last_file != "..") {
            echo "<a href='../subjects/" . strtolower($name_of_last_file) . "'>". $name_of_last_file .'</a><br>';
        }
    }
    echo '</ol>'; 
}?>

you should also use http://php.net/manual/en/function.htmlspecialchars.php to make sure the inserted string is save to use in HTML 您还应该使用http://php.net/manual/en/function.htmlspecialchars.php来确保所插入的字符串已保存以在HTML中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM