JustinMs66@hotmail.com Posted September 9, 2006 Share Posted September 9, 2006 ok so like right now, this code will display the directories in the "files" directory in a select box. but the first 2 options are "." and ".." (without quotes), and THEN the folders. so how do i get rid of those .. things. ???here is my code:[code]<?php$dirpath = "files/"; $dlist = opendir($dirpath); $info = "<select name='dir'>";while ($read = readdir($dlist)) {$info.="<option value='$read'>$read</option>";}closedir($dlist); $info.="</select>";echo $info;?>[/code]and it produces this:[img]http://www.csscobalt.com/uploads/help_php_v01_01.bmp[/img] Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 9, 2006 Share Posted September 9, 2006 Here is an example from the manual on [url=http://us3.php.net/manual/en/function.readdir.php]readdir[/url][code]<?phpif ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($handle);}?> [/code]Hope this helps,Tom Quote Link to comment Share on other sites More sharing options...
tleisher Posted September 9, 2006 Share Posted September 9, 2006 Tom beat me to it, but that's how I would do it. Also, the reason that there is . and .. is because those are the directories you go to go back a directory or two.. so yeah you don't want to have those hanging around the public. Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 10, 2006 Author Share Posted September 10, 2006 wul when i do it this way, it dosn't come up in a select box. it comes up in text. how do i make it come up in a select box, like in my above code? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 10, 2006 Share Posted September 10, 2006 Just replace these lines in your script:[code]<?phpwhile ($read = readdir($dlist)) {$info.="<option value='$read'>$read</option>";}?>[/code]with[code]<?phpwhile (false !== ($read = readdir($dlist))) { if ($read != "." && $read != "..") $info.="<option value='$read'>$read</option>";}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 10, 2006 Author Share Posted September 10, 2006 k awesome :D thnx man!but i need to know....what is the code for if u wana insert a PHP code into a HTML doc?and i tried:[code=php:0]<?php//php code goes here?>[/code]but that didn't work AT all. so how do i do it? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 10, 2006 Share Posted September 10, 2006 That will work if your HTML file is named "something.php". If it is named "something.html", the PHP processor will not get invoked.Ken Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 10, 2006 Author Share Posted September 10, 2006 well it's named "something.HTML" so is there a way to insert PHP into a html doc? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 10, 2006 Share Posted September 10, 2006 Rename it to "something.php".If you're using the Apache web server and have access to the httpd.conf file, you can modify it to make all *.html files go through PHP first, but that is overkill.Use Javascript with AJAX to invoke your PHP script and return what you want to insert, then the Javascript can insert it into the document.It's best just to rename the file.I've gotten into the habit of naming all my files something.php, so I don't have to deal with this problem, even if there is no PHP in them at all, which is very rare these days.Ken Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 10, 2006 Author Share Posted September 10, 2006 ok so i used this: [code] <?php$dirpath = "uploads/cats/"; $dlist = opendir($dirpath); $info = "<select name='dir'>";readdir($dlist);readdir($dlist);while ($read = readdir($dlist)) { $info.="<option value='$read'>$read</option>";}closedir($dlist); $info.="</select>";echo $info;?>[/code]and i JUST realized that it shows FILES as WELL as directories. so how do i make it ONLY show directories? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 10, 2006 Share Posted September 10, 2006 Use the function is_dir() http://www.php.net/is_dir to test whether the file is a directory or not.Ken Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted September 10, 2006 Author Share Posted September 10, 2006 ok so like what, would i do something like this?would i replace this:[b]readdir($dlist);[/b]with this?[b]readdir(is_dir($dlist));[/b] Quote Link to comment Share on other sites More sharing options...
tomfmason Posted September 10, 2006 Share Posted September 10, 2006 No. Try something like this.[code] <?php$dirpath = "uploads/cats/"; $dlist = opendir($dirpath); $info = "<select name='dir'>";while (false !== ($read = readdir ($dlist))) { if ($read != '.' && $read != '..' && is_dir($read)) { $info.="<option value='$read'>$read</option>"; }}closedir($dlist); $info.="</select>";echo $info;?>[/code]Hope this helps,Tom 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.