tqla Posted January 8, 2010 Share Posted January 8, 2010 Hello. I am using this code to look in a directory and echo out the contents: <?php $mydir = "images/home/"; $d = dir($mydir); while($entry = $d->read()) { if ($entry!= "." && $entry!= "..") { echo "'" . $entry . "': { caption: '' }," ; } } $d->close(); ?> In the folder are file names like 1.jpg, 1t.jpg, 2.jpg, 2t.jpg, 3.jpg, 3t.jpg, and so on. I only wish to echo out the files that do NOT have a "t" in their name. Just 1.jpg, 2.jpg, 3.jpg and so on. Can somebody show me how to add this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/ Share on other sites More sharing options...
wildteen88 Posted January 8, 2010 Share Posted January 8, 2010 Does all files that have a 't' in them always in this format [some number here]t.jpg? If they are then you can do this: <?php $mydir = "images/home/"; $d = dir($mydir); while($entry = $d->read()) { if ($entry!= "." && $entry != ".." && substr($entry, -4, 1) != 't') { echo "'" . $entry . "': { caption: '' }," ; } } $d->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991191 Share on other sites More sharing options...
gerardcorr Posted January 8, 2010 Share Posted January 8, 2010 if(!strpos($filename, 't')){ //dont contain t } Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991193 Share on other sites More sharing options...
mrMarcus Posted January 8, 2010 Share Posted January 8, 2010 i'm guessing the 't' represents 'thumbnail' perhaps? you should normalize your directories meaning, have a directory for full-size images, and one for thumbs. assuming this is the case. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991194 Share on other sites More sharing options...
monkeytooth Posted January 8, 2010 Share Posted January 8, 2010 using pathinfo() and breaking the files into an array of info like filename, extension type, filesieze, etc.. And then searching the arrays filename key to see if "t" is in the string might do it.. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991204 Share on other sites More sharing options...
tqla Posted January 8, 2010 Author Share Posted January 8, 2010 wildteen. thanks but I just tried your solution and the files with "t" still show up like this: '1.jpg': { caption: '' },'10.jpg': { caption: '' },'10t.jpg': { caption: '' },'11.jpg': { caption: '' },'11t.jpg': { caption: '' },'12.jpg': { caption: '' }, and so on... mrMarcus, yes "t" is for thumbnail. I was saving the seperate folder idea for if it can't be done through a bit a php code. gerardcorr, where would I put the !strpos code? I tried it in the while loop and in the if statement but no go. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991206 Share on other sites More sharing options...
tqla Posted January 8, 2010 Author Share Posted January 8, 2010 wildteen, your solution works! I just had to change it to substr($entry, -5, 1) Thanks everybody for your help too. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991211 Share on other sites More sharing options...
mrMarcus Posted January 8, 2010 Share Posted January 8, 2010 mrMarcus, yes "t" is for thumbnail. I was saving the seperate folder idea for if it can't be done through a bit a php code. it can be done, yes, but in the future, normalization is the way to go to avoid headaches such as this. if i were you, i'd take the time to make the changes now, but that's just me. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991212 Share on other sites More sharing options...
tqla Posted January 8, 2010 Author Share Posted January 8, 2010 I hear you Marcus. It's just that I built an uploader that automatically creates a thumbnail and puts it in the same folder and I don't want to mess with that anymore. But I do agree. Quote Link to comment https://forums.phpfreaks.com/topic/187740-help-not-echoing-files-with-a-certain-letter-them/#findComment-991215 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.