Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:在结果中使用scandir bu排除..//_Php_Php 5.6 - Fatal编程技术网

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

PHP:在结果中使用scandir bu排除..//,php,php-5.6,Php,Php 5.6,我使用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[]\"

我使用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>";
        }
$dir=scandir('/user1/caravans/public_html/wordpress/wp content/uploads/wpallimport/files');
foreach($dir作为$directory)
{
echo“
$directory
”; }
问题是脚本也会回显“.”和“.”(没有语音标记),有没有一种优雅的方法来删除这些标记?短的或正则表达式。感谢

如果目录是
我建议查看一下控制结构

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

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

您可以使用
数组_diff
删除这些目录:

$dir = scandir($path);
$dir = array_diff($dir, array('.', '..'));
foreach($dir as $entry) {
    // ...
}

除了swidmann的答案之外,另一个解决方案是在迭代之前删除“.”和“…”

改编自

$path='/user1/caravans/public_html/wordpress/wp content/uploads/wpallimport/files';
$exclude=[','..'];
$dir=数组_diff(scandir($path),$exclude);
foreach($dir作为$directory){
// ...
}

这样,如果将来需要,您还可以轻松地将其他目录和文件添加到排除列表中。

谢谢,这是一种非常有用的循环过滤方法。这是一个很好的答案,请格式化它,以便新来者了解continue在if()中,而echo不在if()中。
if( $directory == '.' || $directory == '..' ) continue;
$dir = scandir($path);
$dir = array_diff($dir, array('.', '..'));
foreach($dir as $entry) {
    // ...
}