Jump to Content

RSS Feeds:

Posts | Comments

http://ivanwlam.com/experiments/programming

One Response to “Displaying Images from Any Directory”

  1. [...] Helper PHP File in the code is the packaged version of Displaying Images from a Directory, which I just demo’d in the previous [...]

Leave a Reply


Example

Directory to examine: http://ivanwlam.com/experiments/files/programming/2009/090713-readdir/screenSlideShow/

First: read off all the entries in the directory.

file or directory name: . || filetype: dir || This is not a file.
file or directory name: .. || filetype: dir || This is not a file.
file or directory name: screenBank || filetype: dir || This is not a file.
file or directory name: img01.jpg || filetype: file || This is a file! || filesArray count: 1
file or directory name: img02.jpg || filetype: file || This is a file! || filesArray count: 2
file or directory name: img03.jpg || filetype: file || This is a file! || filesArray count: 3
file or directory name: phpfile.php || filetype: file || This is a file! || filesArray count: 4
file or directory name: _notes || filetype: dir || This is not a file.
file or directory name: liveConstBucket.png || filetype: file || This is a file! || filesArray count: 5
file or directory name: workNighterWPre.swf || filetype: file || This is a file! || filesArray count: 6
file or directory name: index.php || filetype: file || This is a file! || filesArray count: 7
file or directory name: liveConstCodeAnimated.gif || filetype: file || This is a file! || filesArray count: 8
file or directory name: 081102-125x20grid.png || filetype: file || This is a file! || filesArray count: 9

filesArray count total: 9

Second: Only show the image-based files

fileArray index: 0 || file type index: 2 || Image Type: JPEG || imgArray count: 1


fileArray index: 1 || file type index: 2 || Image Type: JPEG || imgArray count: 2


fileArray index: 2 || file type index: 2 || Image Type: JPEG || imgArray count: 3


fileArray index: 3 || file type index: || Image Type: || imgArray count: still 3
This is not an image file.

fileArray index: 4 || file type index: 3 || Image Type: PNG || imgArray count: 4


fileArray index: 5 || file type index: 13 || Image Type: SWC || imgArray count: 5
The file (workNighterWPre.swf) is not a GIF, JPG, nor a PNG

fileArray index: 6 || file type index: || Image Type: || imgArray count: still 5
This is not an image file.

fileArray index: 7 || file type index: 1 || Image Type: GIF || imgArray count: 6


fileArray index: 8 || file type index: 3 || Image Type: PNG || imgArray count: 7


imgArray count total: 7

PHP in Example

 /* Notes on addresses  These functions take the document root and not the uri:      opendir();      filetype();      getimagesize();        */    //get names of image types into an array since exif_imagetype doesn't work for php 4  $imageTypes=array(  "PLACEHOLDER", //for index=0  "GIF",  "JPEG",  "PNG",  "SWF",  "PSD",  "BMP",  "TIFF_II",  "TIFF_MM",  "JPC",  "JP2",  "JPX",  "JB2",  "SWC",  "IFF",  "WBMP",  "XBM");    $path=$_SERVER['DOCUMENT_ROOT']."/experiments/files/programming/2009/090713-readdir/screenSlideShow/";    echo "Directory to examine: http://ivanwlam.com/experiments/files/programming/2009/090713-readdir/screenSlideShow/";    echo "<br />";    //open directory  $dir=opendir($path);  echo "<br />First: read off all the entries in the directory.";  echo "<br /><br />";    //init counter for number of files and not directories  $fileCount=0;  //init array to store file names and not directories  $filesArray=array();    //examine each entry in the directory and store the files in an array.  while($f=readdir($dir)){      echo "file or directory name: $f ||";      echo " filetype: ".filetype($path.$f)." || ";            //check the filetype(). it should return either "file" or "dir"      if(filetype($path.$f)=="file"){          //add file name to array only if it's a file          $filesArray[$fileCount]=$f;          echo " This is a file!";                    //advance the counter          $fileCount++;                    echo " || filesArray count: ".$fileCount;          } else {          echo " This is not a file.";          }      echo "<br />\n";      }    echo "<br />filesArray count total: ".count($filesArray);    echo "<br /><br />Second: Only show the image-based files";  echo "<br />";        //init array to store image files only, not just any files  //coulda combined the process with the above loop, but just wanted to do it right in the demo.  $imgArray=array();    //go through each file from fileArray to determine if it's an image  //if it's an image, show it.  foreach($filesArray as $key => $file){        //the getimagesize() function returns an array of 7 values, including width, height and image type      $getImgSize=getimagesize($_SERVER['DOCUMENT_ROOT']."/experiments/files/programming/2009/090713-readdir/screenSlideShow/".$file);      echo "<br />\nfileArray index: ".$key." || file type index: ".$getImgSize[2]." ";      echo "|| Image Type: ".$imageTypes[$getImgSize[2]];            //if the imagetype returns a value from 1 to 16, which are the indices for the image type catalog, meaning it's an image file      if($getImgSize[2]>0 && $getImgSize[2]<=16){          $imgArray[]=$file;          echo " || imgArray count: ".count($imgArray);          //if the image type is gif(index 1), jpeg (2), or png(3), show the image          if($getImgSize[2]>0 && $getImgSize[2]<=3){              echo "<br /><img src='http://www.ivanwlam.com/experiments/files/programming/2009/090713-readdir/screenSlideShow/".$file."' />";              } else {              echo "<br />The file (".$file.") is not a GIF, JPG, nor a PNG";              }          } else {          echo " || imgArray count: still ".count($imgArray);          echo "<br />This is not an image file.";          }      echo "<br />";      }        echo "<br />imgArray count total: ".count($imgArray);