www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 16 of 16

Thread: Can't read images in from directory using PHP

  1. #16
    Join Date
    Apr 2010
    Posts
    88
    Try this (not very well-tested) code.

    PHP Code:
    function GetImages($dir$basedir null, array $extensions$recursive true)
    {
            
    $dir rtrim($dir'/') . '/';
            
    $images = array();

            if (
    is_dir($dir) && ($dh = @opendir($dir)))
            {
                    while ((
    $file readdir($dh)) !== false)
                    {
                            if (
    $file == '.' || $file == '..')
                                    continue;

                            if (
    $recursive)
                            {
                                    if (
    is_dir($dir $file))
                                    {
                                            
    $nf GetImages($dir $file$basedir$extensions$recursive);
                                            if (
    count($nf))
                                                    
    $images array_merge($images$nf);
                                    }
                                    else
                                    {
                                            if ((
    $pos strrpos($file'.')) !== false)
                                            {
                                                    
    $ext substr($file$pos+1);
                                                    if (
    in_array($ext$extensions))
                                                    {
                                                            if (
    $basedir !== null)
                                                                    
    $images[] = str_replace($basedir''$dir $file);
                                                            else
                                                                    
    $images[] = $dir $file;
                                                    }
                                            }
                                    }
                            }
                            else
                            {
                                    if ((
    $pos strrpos($file'.')) !== false)
                                    {
                                            
    $ext substr($file$pos 1);
                                            if (
    in_array($ext$extensions))
                                            {
                                                    if (
    $basedir !== null)
                                                            
    $images[] = str_replace($basedir''$dir $file);
                                                    else
                                                            
    $images[] = $dir $file;
                                            }
                                    }
                            }
                    }
                    
    closedir($dh);
            }
            return 
    $images;
    }

    $basedir dirname(__FILE__);
    $dir $basedir '/wp-content/uploads';
    $extensions = array('jpg''png''gif');
    $images GetImages($dir$basedir$extensions);
    foreach(
    $images as $img)
            echo 
    '<img src="' $img '" /><br />'
    Last edited by gvre; 02-19-2013 at 09:46 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles