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 ! Link to comment https://forums.phpfreaks.com/topic/132044-list-only-some-extention-file-listing/ 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. Link to comment https://forums.phpfreaks.com/topic/132044-list-only-some-extention-file-listing/#findComment-686185 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> Link to comment https://forums.phpfreaks.com/topic/132044-list-only-some-extention-file-listing/#findComment-688222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.