PHP – Use glob() to search for specific file-names in a directory

PHP – Use glob() to search for specific file-names in a directory
/*
Folder with image files contains:

../test_images_folder/21548.JPG
../test_images_folder/21548_1.JPG
../test_images_folder/21548_2.JPG
../test_images_folder/21548_3.JPG
../test_images_folder/21548_4.JPG
../test_images_folder/21548_5.JPG
../test_images_folder/21548_6.JPG
../test_images_folder/22222_5.JPG

*/



$image = "21548";// string to match image file name

foreach(glob("../test_images_folder/*".$image."*") as $filename){

     echo "$filename";

}

/*
Above ‘glob() usage will return:

../test_images_folder/21548.JPG
../test_images_folder/21548_1.JPG
../test_images_folder/21548_2.JPG
../test_images_folder/21548_3.JPG
../test_images_folder/21548_4.JPG
../test_images_folder/21548_5.JPG
../test_images_folder/21548_6.JPG

*/
[SHORTCODE_Insert_Google_Adsence_Here]
 

Regards,

Ken

Leave a Reply