read images of directory


 / Published in: PHP
 

URL: http://superpixel.ch/projects/imageload/

  1. $folder = opendir("."); // Use 'opendir(".")' if the PHP file is in the same folder as your images. Or set a relative path 'opendir("../path/to/folder")'.
  2.  
  3. $pic_types = array("jpg", "jpeg", "gif", "png");
  4.  
  5. $index = array();
  6.  
  7. while ($file = readdir ($folder)) {
  8.  
  9. if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types))
  10. {
  11. array_push($index,$file);
  12. }
  13. }
  14.  
  15. closedir($folder);

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Vindexus on September 15, 2010

Using strpos to find the extension can lead to bugs if you have a filename similar to "myimage.2010.png" because that will return "2010" instead of "png".

It's better to use this:

$parts = explode(".", $file);
$ext = strtolower($parts[count($parts) - 1]);

if(in_array($ext, $pic_types))
{
    array_push($index, $file);
}
Posted By: reverend on May 11, 2011

Would the php glob function be of better use here?

`glob ('/path/to/directory/' . '*.{jpg,jpeg,gif, png}', GLOB_BRACE)'

Posted By: reverend on May 11, 2011

Would the php glob function be of better use here?

`glob ('/path/to/directory/' . '*.{jpg,jpeg,gif, png}', GLOB_BRACE)'

Posted By: johnmbachu on May 22, 2011

Hi i have this script that allows me to upload jpg or gif files onto mysql database , but, i couldn`t get my script right to show the uploaded photos on the browser. So, please i would want someone to tell me what is wrong we my output script below and probably correct it for me.

Thanks John Mbachu [email protected] +2348021171733.

MY WELL WORKING CODE.

(image_upload.php)

Upload Page

    Name: 



        Upload image:  

See all files

You need to login to post a comment.