Page 1 of 1

Strange Foreach Problem

Posted: Tue Apr 25, 2006 2:47 pm
by jwalsh
Seeing unexpected results with the following code.

Code: Select all

// READ ALL FILES FROM DIRECTORY
			$directory = scandir($filepath);
			var_dump($directory);
			foreach ($directory as $file) 
			{	
				echo $file . "\n";
			}
var_dump shows 2 files in the directory, plus "." and "..", but the foreach isn't iterating the last element.

Ouput:

Code: Select all

***** VAR DUMP *****

array(4) {
  [0]=>
  string(1) "."
  [1]=>
  string(2) ".."
  [2]=>
  string(14) "core.class.php"
  [3]=>
  string(18) "registry.class.php"
}

***** FOREACH *****

.
..
core.class.php

works perfect

Posted: Wed Apr 26, 2006 1:09 am
by dibyendrah
works perfect for me. What is the problem there ? I didn't understand the last element you're talking about.

Dibyendra

Posted: Wed Apr 26, 2006 11:26 am
by myoung1620
maybe an alternative will suffice....

Code: Select all

$mydir = "/your_dir";
$dir_handle= opendir($mydir );
while (false !== ($filename = readdir($dir_handle))) {
   print "{$filename} <br />";
}