简体   繁体   English

“。”在哪里? (点)来自使用 PHP 'scandir' 时

[英]Where does "." (dot) come from when using PHP ´scandir´

I'm a bit confused.我有点困惑。
I'm building a PHP function to loop out images in a specified dir.我正在构建一个 PHP 函数来循环出指定目录中的图像。

PHP PHP

$dir = "bilder/".$objekt[0]['objekt_nr']."/thumbnail/";
$thumbnails = scandir($dir);

print_r($thumbnails);

foreach ($thumbnails as $value) {
   echo "<img src='".$dir.$value. "'>";
}

array大批

(
[0] => .
[1] => ..
[2] => bjornc.jpg
[3] => test_bild3.jpg
)

HTML HTML

<img src='bilder/22159/thumbnail/.'>
<img src='bilder/22159/thumbnail/..'>
<img src='bilder/22159/thumbnail/bjornc.jpg'>
<img src='bilder/22159/thumbnail/test_bild3.jpg'>

How can i get rid of theese dots?我怎样才能摆脱这些点?
I guess it´s the directorie dots..我想这是目录点..

UPDATE更新

The most easy way was found in php.net manual在 php.net 手册中找到了最简单的方法

$thumbnails = array_diff(scandir($dir), array('..', '.'));

The dot directory is the current directory.点目录是当前目录。 Dot-dot is the parent directory.点-点是目录。

If you want to create a list of files in a directory you should really skip those two, or really any directory starting with a leading dot (on POSIX systems like Linux and OSX those are supposed to be hidden directories).如果您想在目录中创建文件列表,您应该真正跳过这两个,或者任何以前导点开头的目录(在 POSIX 系统上,如 Linux 和 OSX,这些应该是隐藏目录)。

You can do that by simply check if the first character in the file name is a dot, and if it is just skip it (ie you continue the loop).您可以通过简单地检查文件名中的第一个字符是否为点来实现,如果只是跳过它(即continue循环)。

You can skip it by using in_array as您可以使用in_array作为跳过它

foreach ($thumbnails as $value) {
    if (!in_array($value, array(".", ".."))) {
        echo "<img src='" . $dir . $value . "'>";
    }
}

they are directory and parent directory, they can be removed with following code:它们是目录和父目录,可以使用以下代码删除它们:

    <?php
    $dir = "downloads/";
    if (is_dir($dir)){
        if ($dir_handler = opendir($dir)){
            while (($file = readdir($dir_handler)) !== false){
                if ($file!="."&&$file!="..") {
                    //your code
                }
            }
        closedir($dir_handler);
        }
    }
    ?>

You can also use this你也可以用这个

foreach ($thumbnails as $value) {
   if ( $value !='.' && $value !='..')
   {
     echo "<img src='".$dir.$value. "'>";
   }  
}

If you are looking for a specific file type, such as images, you are better off using glob .如果您正在寻找特定的文件类型,例如图像,最好使用glob
It allows you to pass a pattern of extensions.它允许您传递扩展模式。
This way you can be sure you fetch only the files you are looking for.通过这种方式,您可以确保只获取您要查找的文件。

Example for multiple file types, .jpg and .png多种文件类型的示例, .jpg.png

$dir = "bilder/".$objekt[0]['objekt_nr']."/thumbnail/";
$files = glob("*.{jpg,png}", GLOB_BRACE);

print_r($files);

GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c' GLOB_BRACE - 扩展 {a,b,c} 以匹配 'a'、'b' 或 'c'

Example for a single file type, .jpg单个文件类型的示例, .jpg

$dir = "bilder/".$objekt[0]['objekt_nr']."/thumbnail/";
$files = glob("*.jpg");

print_r($files);

@joachim Pileborg answer is correct, By the way you can also use glob() @joachim Pileborg 答案是正确的,顺便说一句,您也可以使用 glob()

$thumbnails = glob("$dir/*.jpg");

foreach ($thumbnails as $value) {
   echo "<img src='$value'/>";
}

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

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