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

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.

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

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); ?>

I like PHP's glob function.

foreach(glob(IMAGEPATH.'*') as $filename){
    echo basename($filename) . "
";
}
while (($file = readdir($handle)) !== false) {
    if (
        ($file == '.')||
        ($file == '..')
    ) {
        continue;
    }
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;
}

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

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 => $filename structure. To turn off scanning subdirectories, set the recurse option to false

Use glob function.

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

You got all images in array format

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

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

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/

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

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

You can use OPP oriented DirectoryIterator class.

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

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