Colink Posted July 1, 2018 Share Posted July 1, 2018 Hi Firstly I am a "copy and paste" coder, not a real coder. The following code is on an AMP HTML page. It displays all images from a specific folder in an AMP carousel. I know the AMP part is not relevant but I copy all of the code (below) from his section of the page to show context. What currently happens: All images in folder "images/image4" are displayed in the Carousel What I would like to happen: Return all images WHERE file name starts with "thisword" eg thisword1.jpg thisword2.jpg Ignore thisotherword1.jpg Any suggestions would be appreciated. ColinK Existing code <amp-carousel class="xs-12 bannerimg4 " width="16" height="9" layout="responsive" type="slides" controls loop autoplay delay="3000"> <?php $dir = "images/image4"; $handle = opendir(dirname(realpath(__FILE__)).'/images/image4'); while($file = readdir($handle)){ if($file !== '.' && $file !== '..'){ // echo '<img src="images/banner-1040/'.$file.'" border="0" />'; echo '<amp-img src="images/image4/'.$file.'" layout="responsive" class="cover" width="16" height="9" alt="images/banner-1040/'.$file.'"></amp-img>'; } } ?> </amp-carousel> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 1, 2018 Share Posted July 1, 2018 Use glob() function. $images = glob($dir."/thisword*.*"); 1 Quote Link to comment Share on other sites More sharing options...
Colink Posted July 2, 2018 Author Share Posted July 2, 2018 Thanks for your prompt response. Unfortunately my "copy and paste" coding skills were not good enough for this. All images in the folder still display not just those with file names that start with "thisword" This is what I tried (old $dir line commented out, modified version below in bold) <amp-carousel class="xs-12 bannerimg4 " width="16" height="9" layout="responsive" type="slides" controls loop autoplay delay="3000"> <?php // OLD directory code $dir = "images/image4"; $images = glob($dir."/images/image4/thisword*.*"); $handle = opendir(dirname(realpath(__FILE__)).'/images/image4'); while($file = readdir($handle)){ if($file !== '.' && $file !== '..'){ // echo '<img src="images/banner-1040/'.$file.'" border="0" />'; echo '<amp-img src="images/image4/'.$file.'" layout="responsive" class="cover" width="16" height="9" alt="images/banner-1040/'.$file.'"></amp-img>'; } } ?> </amp-carousel> I did try adding thisword*.* to the handle line, but this hides all images Any further help would be appreciated. Colin K Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 2, 2018 Share Posted July 2, 2018 i recommend reading the php.net documentation for glob(), which Barand posted a link to in his post. it contains an example - Example #1 Convenient way how glob() can replace opendir() and friends, which is exactly what you are doing. 1 Quote Link to comment Share on other sites More sharing options...
dalecosp Posted July 2, 2018 Share Posted July 2, 2018 An an extra hint, glob's return type is array Quote Link to comment Share on other sites More sharing options...
Colink Posted July 15, 2018 Author Share Posted July 15, 2018 First of all a thank you to all those who replied, the experience is much more friendly than the usual stack overflow attitude - "If you have not 90% solved your issue and if you do not have expertise "DON'T ASK"" I am still not much further on. eg I cannot see how this code (from the link @barand gave) can be modified to work with his suggestion of "$images = glob($dir."/thisword*.*");" which @barand gave or how to include code for an array as suggested by @dalecosp and make it all work for my AMP carousel. for each (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; ===== I really am just a copy and paste coder so need something closer to my needs to edit. I do understand the philosophy of learn vs being given the answer but the "clues" have not been sufficient for my skills. If further guidance is not available, thanks again for the answers thus far. ColinK Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 15, 2018 Share Posted July 15, 2018 (edited) I"m not sure why all the uses of path names in vars and in the glob call but I think this (with a little thought) should work: $dir = "something here ending with a \ "; $mask = "thisword*.*"; $images = glob ($dir. $mask); if (count($images) == 0) echo "No images matching $dir.$mask"; else { // example lifted from the Manual under Glob() foreach ($images as $filename) { echo "$filename size: " . filesize($filename) . "\n"; } } Edited July 15, 2018 by ginerjm Quote Link to comment Share on other sites More sharing options...
Colink Posted July 22, 2018 Author Share Posted July 22, 2018 Thanks for your suggestion @ginerjm I will park this issue for now and come back to it when I have other parts of the project completed. Thanks to all who replied. ColinK Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.