Strange Foreach Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Strange Foreach Problem

Post 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
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

works perfect

Post by dibyendrah »

works perfect for me. What is the problem there ? I didn't understand the last element you're talking about.

Dibyendra
myoung1620
Forum Newbie
Posts: 3
Joined: Tue Apr 18, 2006 2:15 pm
Location: VA

Post 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 />";
}
Post Reply