Sasha
Sasha

Reputation: 8705

PHP - remove items that are not image from array

I have this function to get items from directory:

if ($dh = opendir($dir)) {
                $images = array();

                while (($file = readdir($dh)) !== false) {
                    if (!is_dir($dir.$file)) {
                        $images[] = $file;
                    }
                }

                closedir($dh);
            }

        return $images;

The function is working, and I am getting this result (this is a test directory):

array (size=9)
  0 => string 'odluka o matinim podrujima.shs' (length=31)
  1 => string 'Odluka o optinskoj upravi.doc' (length=29)
  2 => string 'o_pirotu3.jpg' (length=13)
  3 => string 'o_pirotu4.jpg' (length=13)
  4 => string 'Panorama 10.jpg' (length=15)
  5 => string 'Panorama 8n.jpg' (length=15)
  6 => string 'Panorama n.jpg' (length=14)
  7 => string 'PRAVILNIK O ORGANIZACIJI I SISTEMATIZACIJI POSLOVA.doc' (length=54)
  8 => string 'Pravilnik_o_reprezentaciji.doc' (length=30)

How can I remove all non images items, and is there some way for me to choose mime type that will stay in returned array (I need jpg, png, and bmp)?

Upvotes: 0

Views: 812

Answers (4)

mdenton8
mdenton8

Reputation: 616

Getting files that are images in PHP is a difficult task. If those are untrusted files, then you had better do a lot of research on how you are going to approach that sanitizing of those images, and in particular, MIME type filtering and extension filtering are just not good enough. Neither is getimagesize(), really.

However, if there is no chance of untrusted files in those directories, then you can simply extension filter, by checking the end of their filenames for their respective extensions. Or, use PHP's getimagesize() function and see if returns anything, but that will give you more than just your specified image formats.

See PHP how can i check if a file is mp3 or image file? for more about MIME-types and detecting images.

Upvotes: 0

Realitätsverlust
Realitätsverlust

Reputation: 3953

Google is your friend ...

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];

    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Rather than removing non-images, why not just not add them in the first place?

...
if( !is_dir($dir.$file)) {
    if( preg_match("/\.(png|gif|jpe?g|bmp)/",$file,$m)) {
        // $m[1] is now the extension of the filename
        // You can perform additional verification
        // Example: if $m[1] == 'png' check if imagecreatefrompng accepts it
        $images[] = $file;
    }
}

Upvotes: 4

JustAPirate
JustAPirate

Reputation: 164

string mime_content_type ( string $filename ) 
Returns the MIME content type for a file as determined by using information from the magic.mime file. 

or

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
Find the numeric position of the first occurrence of needle in the haystack string. 

Are the functions you search.

Upvotes: 0

Related Questions