Utherr12 Posted December 6, 2010 Share Posted December 6, 2010 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 More sharing options...
jeffman Posted December 6, 2010 Share Posted December 6, 2010 You have misunderstood the data type of $d_file. The example show here should help: http://www.php.net/manual/en/directoryiterator.construct.php Link to comment Share on other sites More sharing options...
Utherr12 Posted December 6, 2010 Author Share Posted December 6, 2010 Then why does it work if i replace $images_array[]=$d_file; with only echo $d_file;It shows the file names of all images in that folder. Link to comment Share on other sites More sharing options...
ShadowMage Posted December 6, 2010 Share Posted December 6, 2010 Then why does it work if i replace $images_array[]=$d_file; with only echo $d_file;It shows the file names of all images in that folder.Because of the built-in toString method. Link to comment Share on other sites More sharing options...
Utherr12 Posted December 6, 2010 Author Share Posted December 6, 2010 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 More sharing options...
boen_robot Posted December 6, 2010 Share Posted December 6, 2010 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 More sharing options...
Utherr12 Posted December 6, 2010 Author Share Posted December 6, 2010 Yes, thank you, but right now I'm trying to get used to DirectoryIterator ^^ Link to comment Share on other sites More sharing options...
boen_robot Posted December 6, 2010 Share Posted December 6, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.