Morrison Posted June 14, 2010 Share Posted June 14, 2010 Hi folks, I'm glad to be here. I just started with PHP tonight...I wrote this code that works: <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $string = "$file"; $searchArray = array("_", ".html"); $replaceArray = array(" ", " "); $string = str_replace($searchArray,$replaceArray,$string); echo "<a href=$file>$string\n</a><br/>"; } } closedir($handle); } ?> It looks in a directory, notes what files/folders are in it, then renames them and outputs the results in a list as links to the files. But I want to exclude all files that do not have a .html extension. Can anyone help please? I'm really stuck. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/ Share on other sites More sharing options...
JasonLewis Posted June 14, 2010 Share Posted June 14, 2010 You can easily grab the extension of a file by using this simple snippet: $extension = array_pop(explode(".", $file)); That will return everything after the last period. So, in the case of a filename like: index.html It will return "html". Then you can just check to see of the extension is equal to html. if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "html") { Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1071696 Share on other sites More sharing options...
Morrison Posted June 14, 2010 Author Share Posted June 14, 2010 That is fantastic, works great. Thank you for the quick reply. Now I realize I would like to exclude a couple of specific .html files, is there a way I can do that? I'll bet it involves an array... :'( Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1071705 Share on other sites More sharing options...
JasonLewis Posted June 14, 2010 Share Posted June 14, 2010 heh, yep it sure does. $exclude = array("file1.html","file2.html"); if(!in_array($file, $exclude)){ //do something } Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1071706 Share on other sites More sharing options...
kenrbnsn Posted June 14, 2010 Share Posted June 14, 2010 Why don't you use the glob function to get all ".html" files into an array: <?php $files = glob('*.html'); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1071723 Share on other sites More sharing options...
Morrison Posted June 15, 2010 Author Share Posted June 15, 2010 Thank you ProjectFear I'll try that last bit of code when I'm more awake. You have been a great help. I am confused tho as to where exactly I put it in, all of this in new to me. I've always stuck to CSS, flash, and html. I'm just now getting to PHP and javascript. Here is my code now: <?php $extension = array_pop(explode(".", $file)); if ($handle = opendir('music_pages')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "php") { $string = "$file"; $searchArray = array("_", ".php"); $replaceArray = array(" ", ""); $string = str_replace($searchArray,$replaceArray,$string); echo "<a href=music_pages/$file>$string\n</a><br/>"; } } closedir($handle); } ?> My head hurts just looking at it, lol. Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1072185 Share on other sites More sharing options...
Alex Posted June 15, 2010 Share Posted June 15, 2010 You can easily grab the extension of a file by using this simple snippet: $extension = array_pop(explode(".", $file)); Or you can use the built in function made to do this, pathinfo. echo pathinfo('example.jpg', PATHINFO_EXTENSION); Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1072191 Share on other sites More sharing options...
JasonLewis Posted June 15, 2010 Share Posted June 15, 2010 You can easily grab the extension of a file by using this simple snippet: $extension = array_pop(explode(".", $file)); Or you can use the built in function made to do this, pathinfo. echo pathinfo('example.jpg', PATHINFO_EXTENSION); Or that. Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1072209 Share on other sites More sharing options...
Morrison Posted June 20, 2010 Author Share Posted June 20, 2010 Hey! I've been really busy, just now have time to work on this project. I decided to take my thing to the next level, incorporate a database and automatic page generation. This is the scenario: I have a directory, it has several folders in it, each folder is called "Some_Band_-_Album_Name" and contains all of the songs in that album. Then I have a page with a flash player that opens that folder and plays the song. The way you get to this page is thru a list that is generated automatically from scanning a directory and returning folder names as links to the particular player page. This code makes the page, thanks to ProjectFear and others $dirFiles = array(); // opens music folder if ($handle = opendir('music_pages')) { while (false !== ($file = readdir($handle))) { // strips files extensions $crap = array("php"); $newstring = str_replace($crap, " ", $file ); if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "php") { $dirFiles[] = $file; } } closedir($handle); } sort($dirFiles); foreach($dirFiles as $file) { $string="$file"; $searchArray = array("_", ".php"); $replaceArray = array(" ", ""); $string = str_replace($searchArray,$replaceArray,$string); echo "<li><a href=\"music_pages/$file\">$string</a></li>\n"; } Each player page calls from a folder with this line: flashvars.songInfo = "musik/Arvo_Part_-_de_Profundis"; But now I don't even want to make the player page, I want it echoed out automatically by info in the database. I want a script that: 1.goes to a directory (musik), gets folder names 2.insert these folder names into database with values equal to the names 3.now I will echo out the new page's html code, inserting the needed value into the flashvar. 4.And then print out a list of the folders as links. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/204703-how-to-exclude-file-types/#findComment-1074759 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.