使用 PHP 列出目录中的所有图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3738270/
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 10:56:10  来源:igfitidea点击:

Listing all images in a directory using PHP

phpimage

提问by Drewdin

I have the code below that lists all the images in a folder, the problem is that it finds some files ( a . and a ..)that I am not sure what they are so I am not sure how to prevent them from showing up. I am on a windows XP machine, any help would be great, thanks.

我有下面的代码列出了文件夹中的所有图像,问题是它找到了一些文件(a . 和 ..),我不确定它们是什么,所以我不确定如何防止它们出现. 我在Windows XP 机器上,任何帮助都会很棒,谢谢。

Errors: Warning: rename(images/.,images/.) [function.rename]: No error in C:\wamp\www\Testing\listPhotosA.php on line 14

Warning: rename(images/..,images/..) [function.rename]: No error in C:\wamp\www\Testing\listPhotosA.php on line 14

错误:警告:rename(images/.,images/.) [function.rename]:第 14 行 C:\wamp\www\Testing\listPhotosA.php 中没有错误

警告:rename(images/..,images/..) [function.rename]:C:\wamp\www\Testing\listPhotosA.php 第 14 行没有错误

Code:

代码:

<?php
define('IMAGEPATH', 'images/');

if (is_dir(IMAGEPATH)){
    $handle = opendir(IMAGEPATH);
}
else{
    echo 'No image directory';
}

$directoryfiles = array();
while (($file = readdir($handle)) !== false) {
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;
}

foreach($directoryfiles as $directoryfile){
    if(strlen($directoryfile) > 3){
    echo '<img src="' . IMAGEPATH . $directoryfile . '" alt="' . $directoryfile . '" /> <br>';
    }
}

closedir($handle); ?>

回答by Rocket Hazmat

I like PHP's globfunction.

我喜欢 PHP 的glob功能。

foreach(glob(IMAGEPATH.'*') as $filename){
    echo basename($filename) . "\n";
}

回答by nodws

glob()is case sensitive and the wildcard *will return all files, so I specified the extension here so you don't have to do the filtering work

glob()区分大小写,通配符*将返回所有文件,所以我在这里指定了扩展名,这样你就不必做过滤工作

$d = 'path/to/images/';
foreach(glob($d.'*.{jpg,JPG,jpeg,JPEG,png,PNG}',GLOB_BRACE) as $file){
    $imag[] =  basename($file);
}

回答by Nathan Srivi

Use glob function.

使用 glob 函数。

<?php
define('IMAGEPATH', 'images/');
foreach(glob(IMAGEPATH.'*') as $filename){
    $imag[] =  basename($filename);
}
print_r($imag);
?>

You got all images in array format

你得到了数组格式的所有图像

回答by sagits

To get all jpg images in all dirs and subdirs inside a folder:

要获取文件夹内所有目录和子目录中的所有 jpg 图像:

function getAllDirs($directory, $directory_seperator)
{

$dirs = array_map(function ($item) use ($directory_seperator) {
    return $item . $directory_seperator;
}, array_filter(glob($directory . '*'), 'is_dir'));

foreach ($dirs AS $dir) {
    $dirs = array_merge($dirs, getAllDirs($dir, $directory_seperator));
}
return $dirs;
}



function getAllImgs($directory)
 {
  $resizedFilePath = array();
  foreach ($directory AS $dir) {

    foreach (glob($dir . '*.jpg') as $filename) {

        array_push($resizedFilePath, $filename);

    }

}
  return $resizedFilePath;
}


 $directory = "C:/xampp/htdocs/images/";
 $directory_seperator = "/";

 $$allimages = getAllImgs(getAllDirs($directory, $directory_seperator));

回答by balupton

Using balphp's scan_dir function: https://github.com/balupton/balphp/blob/765ee3cfc4814ab05bf3b5512b62b8b984fe0369/lib/core/functions/_scan_dir.funcs.php

使用balphp的scan_dir函数:https: //github.com/balupton/balphp/blob/765ee3cfc4814ab05bf3b5512b62b8b984fe0369/lib/core/functions/_scan_dir.funcs.php

scan_dir($dirPath, array('pattern'=>'image'));

scan_dir($dirPath, array('pattern'=>'image'));

Will return an array of all files that are images in that path and all subdirectories, using a $path => $filenamestructure. To turn off scanning subdirectories, set the recurseoption to false

将使用结构返回该路径和所有子目录中的所有图像文件的数组$path => $filename。要关闭扫描子目录,请将recurse选项设置为false

回答by Liz Eipe C

Please use the following code to read images from the folder.

请使用以下代码从文件夹中读取图像。

function readDataFromImageFolder() {

$imageFolderName = 14;
$base = dirname(__FILE__);
$dirname = $base.DS.'images'.DS.$imageFolderName.DS;
$files = array();

if (!file_exists($dirname)) {
    echo "The directory $dirname not exists.".PHP_EOL;
    exit;
} else {
    echo "The directory $dirname exists.".PHP_EOL;
    $dh  = opendir( $dirname );

    while (false !== ($filename = readdir($dh))) {
        if ($filename === '.' || $filename === '..') continue;
        $files[] = $dirname.$filename;
    }
    uploadImages( $files );
}
}

Please click here for detailed explanation. http://www.pearlbells.co.uk/code-snippets/read-images-folder-php/

请点击此处查看详细说明。 http://www.pearllbells.co.uk/code-snippets/read-images-folder-php/

回答by Narek

You can use OPP oriented DirectoryIteratorclass.

您可以使用面向 OPP 的DirectoryIterator类。

foreach (new DirectoryIterator(IMAGEPATH) as $fileInfo) {
    // Removing dots
    if($fileInfo->isDot()) {
        continue;
    }

    // You have all necessary data in $fileInfo
    echo $fileInfo->getFilename() . "<br>\n";
}

回答by Sergey Eremin

while (($file = readdir($handle)) !== false) {
    if (
        ($file == '.')||
        ($file == '..')
    ) {
        continue;
    }
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;
}