$imageString = ""; $folder_count = 0; foreach (glob("images/*", GLOB_ONLYDIR) as $folder_name) { if ($folder_count > 0) { $imageString .= "||"; } $imageString .= str_replace("images/", "", $folder_name) . "|"; $image_count = 0; foreach (glob("$folder_name/{*.jpg,*.JPG}", GLOB_BRACE) as $image) { if ($image_count > 0) $imageString .= ","; $imageString .= str_replace("$folder_name/", "", $image); $image_count++; } $folder_count++; } ?>