Search code examples
phpjavascriptglob

Making file extention as a css class by using glob function


My PHP knowledge is kind of limited, and I was trying to search for different PHP/JavaScript solutions, but nothing helped me to make it work, so I'd just try to ask the question directly.

I have the following PHP code that loads all the images from a specific folder on server:

foreach(glob($images_path . '*.*') as $filename){
echo '<img src="' . $filename . '" class="preview" />';
}

Now, in order to give different styles to the images, based on their kind, I want to recognize the extension of the file, whether with JavaScript/jQuery or PHP, and to put it as a class (preferably, but could be some other idea)... let's say, after the class "preview" which I already have in the code. It could be similar to the extension, just without the dot, of course; e.g. class="preview png".

Thank you very much in advance!


Solution

  • foreach(glob($images_path . '*.*') as $filename){
       $ext = pathinfo($filename, PATHINFO_EXTENSION);
       echo '<img src="' . $filename . '" class="preview ' . $ext . '" />';
    }