php 如何在PHP中使用scandir仅获取图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3226519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:04:12  来源:igfitidea点击:

How to get only images using scandir in PHP?

phpdirectory-structurefile-extensionscandirimage-file

提问by esafwan

Is there any way to get only images with extensions jpeg, png, gifetc while using

有没有什么办法让仅与扩展的图像jpegpnggif等同时使用

$dir    = '/tmp';
$files1 = scandir($dir);

回答by Gordon

You can use glob

您可以使用 glob

$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);

If you need this to be case-insensitive, you could use a DirectoryIteratorin combination with a RegexIteratororpass the result of scandirto array_mapand use a callback that filters any unwanted extensions. Whether you use strpos, fnmatchor pathinfoto get the extension is up to you.

如果您需要不区分大小写,您可以将 aDirectoryIterator与 a 结合使用RegexIterator传递scandirto的结果array_map并使用过滤任何不需要的扩展名的回调。是使用strposfnmatch还是pathinfo获取扩展名取决于您。

回答by Rajesh Kumar R

The actual question was using scandir and the answers end up in glob. There is a huge difference in both where blob considerably heavy. The same filtering can be done with scandir using the following code:

实际问题是使用 scandir,答案最终以 glob 形式出现。两者之间存在巨大差异,其中 blob 相当重。可以使用以下代码使用 scandir 完成相同的过滤:

$images = preg_grep('~\.(jpeg|jpg|png)$~', scandir($dir_f));

I hope this would help somebody.

我希望这会帮助某人。

回答by Josiah

I would loop through the files and look at their extensions:

我会遍历文件并查看它们的扩展名:

$dir = '/tmp';
$dh  = opendir($dir);
while (false !== ($fileName = readdir($dh))) {
    $ext = substr($fileName, strrpos($fileName, '.') + 1);
    if(in_array($ext, array("jpg","jpeg","png","gif")))
        $files1[] = $fileName;
}
closedir($dh);

回答by Madan Sapkota

Here is a simple way to get only images. Works with PHP >= 5.2version. The collection of extensions are in lowercase, so making the file extension in loop to lowercase make it case insensitive.

这是一种仅获取图像的简单方法。适用于PHP >= 5.2版本。扩展名的集合是小写的,因此将循环中的文件扩展名设为小写使其不区分大小写。

// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

// init result
$result = array();

// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');

// iterate
foreach ($directory as $fileinfo) {
    // must be a file
    if ($fileinfo->isFile()) {
        // file extension
        $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
        // check if extension match
        if (in_array($extension, $extensions)) {
            // add to result
            $result[] = $fileinfo->getFilename();
        }
    }
}
// print result
print_r($result);

I hope this is useful if you want case insensitive and image only extensions.

如果您想要不区分大小写和仅图像扩展名,我希望这很有用。

回答by Borealid

You can search the resulting array afterward and discard files not matching your criteria.

您可以随后搜索结果数组并丢弃与您的条件不匹配的文件。

scandirdoes not have the functionality you seek.

scandir没有您寻求的功能。

回答by Aaron Olin

If you would like to scan a directory and return filenames only you can use this:

如果您想扫描目录并仅返回文件名,您可以使用以下命令:

$fileNames = array_map(
    function($filePath) {
        return basename($filePath);
    },
    glob('./includes/*.{php}', GLOB_BRACE)
);

scandir()will return .and ..as well as the files, so the above code is cleaner if you just need filenames or you would like to do other things with the actual filepaths

scandir()将返回...文件一样,所以如果你只需要文件名或者你想用实际的文件路径做其他事情,上面的代码会更清晰