Wednesday, May 7, 2014

get all images as array from a folder or directory in php

<?php
$myfull_path="/uploads";
$images=array();

if ($dir = opendir( $myfull_path)) {
    $images = array();
    while (false !== ($file = readdir($dir))) {
        if ($file != "." && $file != ".." )
        {
        $ext=strtolower(getExtension($file));
       
        if ($ext=="jpg" || $ext=="jepg" || $ext=="png" || $ext=="gif" || $ext=="bmp" )   
        {

            $images[] = $file;
        }
       
        }
    }
    closedir($dir);
}



print_r($images);

function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

?>

No comments:

Post a Comment