wesley Posted October 21, 2015 Share Posted October 21, 2015 Hello. I have a php script and this show the images from a folder img in the browser. But the images doesn't show in the browser. Can someone help me with the code? My question is: what is wrong on the script? How can I show the images in the browser? thanks in advance. my code is: <?phpfunction showGallery(){ $galleryHTML .= "<ul>"; $images = new DirectoryIterator("img"); while($images -> valid() ){ $file = $images->current(); $filename = $file->getFilename(); $src = "img/$filename"; $info = new Finfo( FILEINFO_MIME_TYPE ); $type = $info->file($src); if($type === "image/jpeg"){ $galleryHTML .= "<li><img src='$src' alt='image gallery' /></li>"; } $images->next(); } $galleryHTML .= "</ul>"; return $galleryHTML;}echo showGallery();?> Link to comment Share on other sites More sharing options...
dsonesuk Posted October 21, 2015 Share Posted October 21, 2015 Add $galleryHTML =""; at the very top. And as long as you have a img folder at the same location as this file with this code, AND jpg, jpeg, or jpe images in that img folder, it should work. Link to comment Share on other sites More sharing options...
wesley Posted October 21, 2015 Author Share Posted October 21, 2015 Thank you, But I have add $galleryHTML =""; on the top (see code), you mean this?, and the images (all jpg) are in the img folder and it doesn't work. the images are not to see in the browser. Can you help me? <?phpfunction showGallery(){ $galleryHTML =""; $galleryHTML .= "<ul>"; $images = new DirectoryIterator("img"); while($images->valid() ){ $file = $images->current(); $filename = $file->getFilename(); $src = "img/$filename"; $info = new Finfo( FILEINFO_MIME_TYPE ); $type = $info->file($src); if($type === "image/jpg"){ $galleryHTML .= "<li><img src='$src' alt='image gallery' /></li>"; } $images->next(); } $galleryHTML .= "</ul>"; return $galleryHTML;}echo showGallery();?> Link to comment Share on other sites More sharing options...
dsonesuk Posted October 21, 2015 Share Posted October 21, 2015 But you didn't have $galleryHTML =""; in original post? if the img folder path is relative to the file in question it should work, is it using the same uppercase/lowercase characters because that will make a difference. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 21, 2015 Share Posted October 21, 2015 You're not checking anything. Print out something inside the while loop so you know it's running. Print out the type so you know if the if statement is working. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now