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

时间:2020-12-15 00:34:16

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

有没有办法在使用时只获得带有扩展名jpeg,png,gif等的图像

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

6 个解决方案

#1


77  

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 DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up to you.

如果您需要不区分大小写,可以将DirectoryIterator与RegexIterator结合使用,或者将scandir的结果传递给array_map,并使用可回收任何不需要的扩展的回调。您是否使用strpos,fnmatch或pathinfo来获取扩展名。

#2


22  

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.

我希望这会对某人有所帮助。

#3


12  

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;
}

#4


10  

Here is a simple way to get only images. Works with PHP >= 5.2 version. 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.

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

#5


5  

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

您可以在之后搜索生成的数组,并丢弃与您的条件不匹配的文件。

scandir does not have the functionality you seek.

scandir没有你所寻求的功能。

#6


2  

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()将返回。和..以及文件,所以如果你只需要文件名或者你想用实际的文件路径做其他事情,上面的代码更清晰

#1


77  

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 DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up to you.

如果您需要不区分大小写,可以将DirectoryIterator与RegexIterator结合使用,或者将scandir的结果传递给array_map,并使用可回收任何不需要的扩展的回调。您是否使用strpos,fnmatch或pathinfo来获取扩展名。

#2


22  

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.

我希望这会对某人有所帮助。

#3


12  

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;
}

#4


10  

Here is a simple way to get only images. Works with PHP >= 5.2 version. 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.

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

#5


5  

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

您可以在之后搜索生成的数组,并丢弃与您的条件不匹配的文件。

scandir does not have the functionality you seek.

scandir没有你所寻求的功能。

#6


2  

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()将返回。和..以及文件,所以如果你只需要文件名或者你想用实际的文件路径做其他事情,上面的代码更清晰