16

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); ?>
VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
Drewdin
  • 1,732
  • 5
  • 23
  • 35

8 Answers8

32

I like PHP's glob function.

foreach(glob(IMAGEPATH.'*') as $filename){
    echo basename($filename) . "\n";
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Great! i didnt realize PHP had a glob, that worked great. I still dont know what the . and .. are that opendir was finding. ANy ideas? I'm just curious for the future – Drewdin Sep 17 '10 at 19:44
  • 1
    '.' is the current directory, and '..' is the parent directory. I don't know why opendir was showing them. – gen_Eric Sep 17 '10 at 19:48
  • What if the imagepath contains brackets i.e.`[byUser]` ? – jscripter Jun 21 '15 at 19:23
  • @BubuDaba: Not sure. Try it. – gen_Eric Jun 22 '15 at 00:48
  • 1
    @RocketHazmat it doesn't work because [glob](http://php.net/manual/es/function.glob.php) parses the string as a regex pattern so regex chars break it. I tried escaping it with [preg_quote](http://php.net/manual/es/function.preg-quote.php) but it didn't work too. I dunno why, I'm not expert – jscripter Jun 22 '15 at 01:43
  • @BubuDaba: You might want to ask that as your own question, see what others have to say. – gen_Eric Jun 22 '15 at 14:59
  • That regex will return all files, and glob is case sensitive so you must do something like `glob($path.'*.{jpg,JPG,jpeg,JPEG,png,PNG}',GLOB_BRACE)` – nodws Aug 16 '17 at 16:05
11

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);
}
nodws
  • 1,018
  • 14
  • 18
6

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

Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
2

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));
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
Renato Probst
  • 5,914
  • 2
  • 42
  • 45
1

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

balupton
  • 47,113
  • 32
  • 131
  • 182
0

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/

Liz Eipe C
  • 265
  • 1
  • 5
0

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>\n";
}
Yohn
  • 2,519
  • 19
  • 23
Narek
  • 3,813
  • 4
  • 42
  • 58
-1
while (($file = readdir($handle)) !== false) {
    if (
        ($file == '.')||
        ($file == '..')
    ) {
        continue;
    }
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;
}
Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44