gpt4 book ai didi

PHP:在结果中使用 scandir bu 排除 .././

转载 作者:行者123 更新时间:2023-12-02 06:31:50 25 4
gpt4 key购买 nike

我正在使用 scandir 和 foreach 循环向用户显示目录中的文件列表。我的代码如下:

        $dir = scandir('/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files');

foreach($dir as $directory)
{
echo "<br/><input type='checkbox' name=\"File[]\" value='$directory'/>$directory<br>";
}

问题是脚本还回显了一个“.”和一个“..”(没有语音标记),有没有一种优雅的方法可以删除这些?简短或正则表达式。谢谢

最佳答案

只是continue如果目录是 ... 我建议看一下控制结构 here

$dir = scandir('/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files');

foreach($dir as $directory) {
if( $directory == '.' || $directory == '..' ) {
// directory is . or ..
// continue will directly move on with the next value in $directory
continue;
}

echo "<br/><input type='checkbox' name=\"File[]\" value='$directory'/>$directory<br>";
}

取而代之的是:

if( $directory == '.' || $directory == '..' ) {
// directory is . or ..
// continue will directly move on with the next value in $directory
continue;
}

你可以使用它的简短版本:

if( $directory == '.' || $directory == '..' ) continue;

关于PHP:在结果中使用 scandir bu 排除 .././,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com