[英]How to get only images using scandir in PHP?
Is there any way to get only images with extensions jpeg
, png
, gif
etc while using有没有办法在使用时只获取带有扩展名jpeg
、 png
、 gif
等的图像
$dir = '/tmp';
$files1 = scandir($dir);
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
If you need this to be case-insensitive, you could use a DirectoryIterator
in combination with a RegexIterator
or pass the result of scandir
to array_map
and use a callback that filters any unwanted extensions.如果您需要它不区分大小写,您可以将DirectoryIterator
与RegexIterator
结合使用,或者将scandir
的结果传递给array_map
并使用过滤任何不需要的扩展名的回调。 Whether you use strpos
, fnmatch
or pathinfo
to get the extension is up to you.是否使用strpos
、 fnmatch
或pathinfo
来获取扩展名取决于您。
The actual question was using scandir and the answers end up in glob.实际问题是使用 scandir,答案最终以 glob 形式出现。 There is a huge difference in both where blob considerably heavy.两者之间存在巨大差异,其中 blob 相当重。 The same filtering can be done with scandir using the following code:可以使用以下代码使用 scandir 完成相同的过滤:
$images = preg_grep('~\.(jpeg|jpg|png)$~', scandir($dir_f));
I hope this would help somebody.我希望这会帮助某人。
Here is a simple way to get only images.这是一种仅获取图像的简单方法。 Works with PHP >= 5.2 version.适用于PHP >= 5.2版本。 The collection of extensions are in lowercase, so making the file extension in loop to lowercase make it case insensitive.扩展名的集合是小写的,因此将循环中的文件扩展名设为小写使其不区分大小写。
// 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.如果您想要不区分大小写和仅图像扩展名,我希望这很有用。
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);
You can search the resulting array afterward and discard files not matching your criteria.您可以随后搜索结果数组并丢弃与您的条件不匹配的文件。
scandir
does not have the functionality you seek. scandir
没有您寻求的功能。
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 .
scandir()
将返回.
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和..
以及文件,所以如果你只需要文件名或者你想用实际的文件路径做其他事情,上面的代码会更清晰
I wrote code reusing and putting together parts of the solutions above, in order to make it easier to understand and use:我编写了代码重用并将上述解决方案的各个部分放在一起,以使其更易于理解和使用:
<?php
//put the absolute or relative path to your target directory
$images = scandir("./images");
$output = array();
$filer = '/(.jpg|.png|.jpeg|.gif|.bmp))/';
foreach($images as $image){
if(preg_match($filter, strtolower($image))){
$output[] = $image;
}
}
var_dump($output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.