Peuplarchie Posted November 9, 2008 Share Posted November 9, 2008 Good day to you all, I'm working on a pice of code which list all files in a directory and return the list and its content right under the name of the file. <?php $thelist = ""; if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="'.$file.'">'.$file.'</a><br/>'; $contents = file($file); $string = implode($contents); $thelist .= '<p>'.$string.'</p>'; } } closedir($handle); } ?> <P>List of files:</p> <P><?=$thelist?></p> I'm looking for a way of choose only 3 or 4 extension only. How would I dow so ? Thanks ! Quote Link to comment Share on other sites More sharing options...
trq Posted November 9, 2008 Share Posted November 9, 2008 Add more conditional expressions to your if statement. eg; if ($file != "." && $file != ".." && substr($file, -1, 4) == '.jpg') Would now obly display files ending with the .jpg extension. Quote Link to comment Share on other sites More sharing options...
ddrudik Posted November 12, 2008 Share Posted November 12, 2008 Since PHP is loaded with functions, here's a way to use a couple of them to shorten your code: <pre> <?php $thelist = ""; foreach (glob("*.{htm,php,txt}",GLOB_BRACE) as $file) { $thelist .= "<a href='$file'>$file</a><br/><p>".htmlentities(file_get_contents($file))."</p>"; } ?> <P>List of files:</p> <P><?=$thelist?></p> 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.