dmccabe Posted March 10, 2008 Share Posted March 10, 2008 I am just playing php, html and css and want to create page where the user can select different css styles to apply to the site. I have already made the site and created 2 different css files an have even managed to make it so the user can select css 1 or css 2, click submit and it applies it to the site. However what would be better is if it could read the list of css files from the root directory and populate the drop down list. I did find an example of a php script to list files, but couldn't work out what exactly it was doing so would prefer to learn it properly. Any takers to give me a well coded example? Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/ Share on other sites More sharing options...
dmccabe Posted March 10, 2008 Author Share Posted March 10, 2008 Update...lol (it's always the same I look at a problem for hours then just after posting I work it out lol). Anyways, using the example I found on the web for getting the list of files, I did this: <? if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="'.$file.'">'.$file.'</a>'; echo "<option value=\"$file\">$file</option>"; } } closedir($handle); } ?> Which does almost what I want, except it lists all the files not just the .css files. Now my question is how do you do a wilcard match in php ? so I can check if $file = *.css or the equivalent. Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488846 Share on other sites More sharing options...
raydawg Posted March 10, 2008 Share Posted March 10, 2008 http://us.php.net/manual/en/ref.pcre.php Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488851 Share on other sites More sharing options...
soycharliente Posted March 10, 2008 Share Posted March 10, 2008 Try... if ($file != "." && $file != ".." && substr($file, -4) == ".css") Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488854 Share on other sites More sharing options...
trq Posted March 10, 2008 Share Posted March 10, 2008 <?php foreach (glob($_SERVER['DOCUMENT_ROOT'] . "/*.css") as $file) { echo "<option value=\"$file\">$file</option>"; } ?> Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488859 Share on other sites More sharing options...
dmccabe Posted March 10, 2008 Author Share Posted March 10, 2008 Thanks for the replies guys. Charlie, yours did the trick nicely, but I think thorpe's would be the correct way if trying to keep to good coding standards. That said I am very new to PHP, html and CSS so would appreciate it if you could explain your code a little Thorpe? Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488876 Share on other sites More sharing options...
trq Posted March 10, 2008 Share Posted March 10, 2008 Its quite simple. glob returns an array of files matching the pattern described in the first argument. You then use a foreach to loop through this array. Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-488897 Share on other sites More sharing options...
soycharliente Posted March 11, 2008 Share Posted March 11, 2008 I just took your code and added something. I didn't want to rewrite your entire code for you. Link to comment https://forums.phpfreaks.com/topic/95495-list-of-files-from-root-directory/#findComment-489195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.