Subscribe For Free Updates!

We'll not spam mate! We promise.

Jun 17, 2013

Get all images from folder - php

Get all images from folder - php:

 The below code is used to get all images from folder in local system. we can set the condition for file type to read files from particular directory in php.

<?php
$imgdir = 'images/'; 
$allowed_types = array('png','jpg','jpeg','gif'); 
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
  if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
          in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
  {$a_img[] = $imgfile;}
}
echo "<ul>";

 $totimg = count($a_img);

 for($x=0; $x < $totimg; $x++)
{
echo "<li><img src='" . $imgdir . $a_img[$x] . "' /></li>";
}
echo "</ul>";


?>



 In the above coe $imgdir is used to pick your directory. $allowed_type is used to set the type of files u allowed from folder to pickup. 

opendir() function is used to open your given folder to read it.

readdir() is used to read folder. the while loop is read all files from folder and push the allowed type files in $a_img array.

Now we can print all image name by using for loop or any other method.


TechniqZone Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment