Jump to content

Simple script which doesn't work


Utherr12

Recommended Posts

I have

$Idirectory = new DirectoryIterator("/var/www/blogger/gallery/thumb");$images_array = array();foreach ($Idirectory as $d_file) {	if(!$Idirectory->isDot())		$images_array[]=$d_file;}foreach ($images_array as $image){	echo "<img src=\"gallery/thumb/".$image."\" alt=\"img\"></img>";}

And it doesn't work. The source code revealed to me that $image is null on every iteration

Link to comment
Share on other sites

Oh, ok hidden things.... I solved the problem anyway...thanks for the help :).

function getImages(DirectoryIterator $dir){	$images_array = array();	foreach ($dir as $d_file) 	{		if(!$d_file->isDot())			$images_array[]=$d_file->getFilename();	}	return $images_array;}$Idirectory = new DirectoryIterator("/var/www/blogger/gallery/thumb");$images_array = getImages($Idirectory);foreach ($images_array as $image){	echo "<img src=\"gallery/thumb/".$image."\" alt=\"img\"></img>";}

If you have any further objections to what i did... i'm more than welcome to hear them.

Link to comment
Share on other sites

You can use scandir() as a shortcut for most of that.If you have PHP 5.3, you can reduce it down to:

$images_array = array_filter(scandir("/var/www/blogger/gallery/thumb"), function($value) { return $value !== '.' && $value !== '..'; });

Link to comment
Share on other sites

OK then, but... you might want to actually iterate over a folder and do something useful with it, instead of reinventing the wheel. An example of something useful is actually echoing the images instead of storing an array with their paths.<< Moved the question below in its own topic: http://w3schools.invisionzone.com/index.php?showtopic=35152 >>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...