开发者

How do I list imags from directory?

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 wit开发者_开发百科h 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.


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜