m_tyhurst2002 Posted November 21, 2011 Share Posted November 21, 2011 Hello guys and gals, I am pretty green to PHP! I have an empty array that that I am trying to put images into. The thing is I have a certain file name in the folder, I want to exclude that file. This is what I have tried, any advice would be appreciated! $thumbImg[] = array(); foreach (glob($DImg) as $PImg) { if (!is_file("thumbnail.jpg")) { $thumbImg[] = "<img src=\"pathtoimage\">"; } } Later on the page I am printing it out with this. It is still including the thumbnail.jpg image. Thank you in advance!! for ($i=0; $i<count($thumbImg); $i++) print $thumbImg[$i]; Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/ Share on other sites More sharing options...
Adam Posted November 21, 2011 Share Posted November 21, 2011 is_file will check if the file exists on the server, nothing related to the current file in your array. You need to compare it directly: if (basename($PImg) != 'thumbnail.jpg') { // ... } Though if you plan on expanding this list, you might want to create an array of exceptions and then check the current file in your loop is not within the exceptions array: $exceptions = array( 'thumbnail.jpg', 'another-file.jpg' ); [...] if (!in_array(basename($PImg), $exceptions)) { // ... } Edit Define your exceptions array before the loop by the way. Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/#findComment-1289939 Share on other sites More sharing options...
m_tyhurst2002 Posted November 21, 2011 Author Share Posted November 21, 2011 Awesome, thank you very much!! It works beautifully with the $exceptions array. I do have one more question. Where I am printing the FOR loop, it outputs the word "array" right before the images. Is the loop incorrect somehow? Thank you so much for your help! <? for ($i=0; $i<count($thumbImg); $i++) print $thumbImg[$i]; ?> Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/#findComment-1290081 Share on other sites More sharing options...
Adam Posted November 21, 2011 Share Posted November 21, 2011 What does the following code display..? for ($i=0; $i<count($thumbImg); $i++) var_dump($thumbImg[$i]); Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/#findComment-1290092 Share on other sites More sharing options...
m_tyhurst2002 Posted November 21, 2011 Author Share Posted November 21, 2011 It outputs, "array(0) { } string(192) " " string(192) " " string(192) "", along with the images. Thanks for the quick reply!! Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/#findComment-1290094 Share on other sites More sharing options...
m_tyhurst2002 Posted November 22, 2011 Author Share Posted November 22, 2011 The empty array was wrong. Instead of this, $thumbImg[] = array(); It's supposed to be, $thumbImg = array(); Thanks for your help Adam!! Link to comment https://forums.phpfreaks.com/topic/251533-if-is-file-print-all-others/#findComment-1290320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.