-1

我目前正在使用此 PHP 脚本生成链接列表(指向单个目录中的文件):

### BEGIN Directory Listing
PHP script to list files in a dir, currently lists dummy files as well, but it works.

<?php
$dir="./content"; // Directory where files are stored
if ($dir_list = opendir($dir)) {
    while(($filename = readdir($dir_list)) !== false) {
        //this kills the annoying .. and . directory listing
        if($filename == ".." || $filename == ".") continue; ?>
    <p><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></p> 
<?php
}
closedir($dir_list);
}
?>
### END Directory Listing

我现在希望只列出文件名长度为 8 个字符的文件(./content 文件夹包含多个长度的文件名,但只有一种文件类型)。还应注意,这些文件不显示/不具有文件扩展名。

一如既往,非常感谢您的帮助和知识!

4

3 回答 3

3

检查长度strlen()

if (strlen($filename) !== 8) continue;
于 2013-01-11T15:34:13.520 回答
0
if ($dir_list = opendir($dir)) {
    while(($filename = readdir($dir_list)) !== false) {
        if(strlen($filename) !== 8) continue;
        //this kills the annoying .. and . directory listing
        if($filename == ".." || $filename == ".") continue; ?>
于 2013-01-11T15:34:30.760 回答
0

strlen()用函数 检查它

if(strlen($filename) ==8)){ //做点什么 }

于 2013-01-11T15:34:55.073 回答