crimsonjade Posted May 11, 2007 Share Posted May 11, 2007 Hi, I'm so sorry if this may have been asked before...but I've been searching and can't seem to find the right topic. I'm looking for a PHP code that will hide (not unset) any filename within a directory that doesn't end in a particular extension (i.e. ".txt") from showing up in an array using foreach. In this case, I am only showing txt files in an array displayed in an HTML selection form element. I'm a total newbie at PHP (taking my first PHP course ever) and am entirely stuck on this. Any help would be most appreciated! Thanks in advance! P.S. If this has been posted before (and I have a feeling it has) maybe a link or a keyword hint would suffice. Thanks again ^^ ~**Crimsonjade**~ Quote Link to comment https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/ Share on other sites More sharing options...
StormTheGates Posted May 11, 2007 Share Posted May 11, 2007 Ok basically I dont know a solution. I can however theorize one: foreach(){ $end = substr('$name', -3, 1); if($end != "txt"){ $i++ } else { echo "$name"; $i++; } } Or something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/#findComment-250284 Share on other sites More sharing options...
crimsonjade Posted May 16, 2007 Author Share Posted May 16, 2007 Actually, I ended up having to do this (as I had tried your suggestion...but then my select box came out blank...): foreach($fileslist as $filenames) { list($name, $extension) = explode('.txt', $filenames); if (is_dir ($directory)){ //I'll think of something to put here later... } else { die ("Directory 'courses' or required files do not exist."); } if (is_dir($filenames)) { continue; } elseif (!ereg("^.*\.(txt)$", $filenames)){ continue; } echo "<option value=\"$filenames\">$name</option><br />"; } But thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/#findComment-254146 Share on other sites More sharing options...
AndyB Posted May 16, 2007 Share Posted May 16, 2007 Use the glob() function - it'll do exactly what you want in one line of code. Quote Link to comment https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/#findComment-254175 Share on other sites More sharing options...
crimsonjade Posted May 19, 2007 Author Share Posted May 19, 2007 Use the glob() function - it'll do exactly what you want in one line of code. My goodness...that's...that's so much simpler...*frowns* unfortunately I'm only supposed to use that which we've learned about in class since this is what the assignment's testing *sigh* But thank you! Definitely will keep that in mind in the future! Quote Link to comment https://forums.phpfreaks.com/topic/50881-solved-hiding-files-within-a-directory/#findComment-257193 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.