views:

29

answers:

2

I'm trying to show all images within a specified directory.

The following code lists all allowed file names:

  function getDirectoryList() 
  {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($this->currentDIR);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

      // Make sure we get allowed images types
      if ($this->allowedFileType($file,$this->allowedImageTypes) )
      {
        $results[] = $file;
      }
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;
  }

  (...)

 $images = getDirectoryList();

 foreach($images as $img) {
   echo "<li>".$img."</li>"; 
 } 

How can I get file size and MIME type? I read that mime_content_typeis deprecated and I should use finfo_file istead. But I've not been very successfull with this.

Do I have to use /usr/share/misc/magic to get file information? Can't I use GD library?

I've looked at many examples, but they are old and don't work that well.

Any help appreciated.

+1  A: 

to get the size and mime type of image its simple,

use function : getimagesize

uses like :

list($width, $height, $type, $attr) = getimagesize("img/myimg.jpg");

Returns an array with 7 elements.

Index 0 and 1 contains respectively the width and the height of the image.

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

using filesize give the size in bytes

Haim Evgi
Thanks. That at least gives me the MIME type. But I need the file size, not the image height / width.
Steven
use filesize php function
Haim Evgi
yup, discovered that.I was just hopeing there might be one file function that hadd all this info.
Steven
A: 

To expand on Haim Evgi's post, use getimagesize() to retrieve the dimensions and the image type in an array. Then, use image_type_to_mime_type() on the image type code to retrieve the MIME:

list ($fileWidth, $fileHeight, $fileType) = getimagesize($filename);
$fileMimeType = image_type_to_mime_type($fileType);
demonkoryu