1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact
Home > PHP Tips > PHP Tip – Find all Images in a Folder

PHP Tip – Find all Images in a Folder

Posted on: 26th Jul 2010 By: Robert Kent Leave a comment 2 Comments

There may come a point in your lives when you need to find some images in a folder. There are many reasons why this may be so, but not many that come to my mind immediately – only this – find all images and organize them for some reason.

Well here is a function that I’ve picked up from somewhere and adapted slightly.

<?php

$namecheck = "apple";

$dir = opendir ("../images");
while (false !== ($file = readdir($dir))) {
if (strpos($file, '.gif',1)||strpos($file, '.jpg',1) ) {

if(strpos($file, $namecheck)){
$myapples[] = $file;
}

}
}

foreach($myapples as $apple){

echo $apple;

}

?>

The above example will find all images in a specific folder with the word “apple” contained in the filename and print them out.

This can adapted in lots of ways so please use it for your own projects.

Thanks for visiting e-commerce website design, magento blog – home of the magento fox!

  • Twitter
  • AIM
  • WordPress
  • LinkedIn
  • Facebook
  • StumbleUpon
  • Technorati Favorites
  • Delicious
  • Ping
  • Digg
  • Bebo
  • Reddit
  • MySpace
  • NewsVine
  • Google Bookmarks
  • Sphinn
  • Propeller
Categories: PHP Tips Tags:

2 Responses to “PHP Tip – Find all Images in a Folder”

  1. EcommerceDeveloper

    What do you think about glob function? It is more efficient way and more flexible.

    $images = glob(‘../images/{*.jpg,*.gif}’, GLOB_BRACE);
    // or apple named images
    $appleImages = glob(‘../images/*apple{*.jpg,*.gif}’, GLOB_BRACE);

  2. Robert Kent

    Thats a nice bit of code – I’ll try it out and let you know if it helps out the script I’m making – cheers for that!

Post A Comment