ztealmax Posted December 14, 2006 Share Posted December 14, 2006 it me again ;Dis it possible to scan a folder and get names of all folders within that folder as a list or a dropdown selection menu?If yes, a small example would be superb :D//CheersMartin Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 Yes. if you just want folders[code]<?php$dir = ".";if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(filetype($file) == "dir"){ echo "$file<br>"; } } }closedir($handle);}?>[/code]If you want them in a dropdown manu[code]<?php$dir = ".";$dir_array=array();if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(filetype($file) == "dir"){ $dir_array[] = $file; } } }closedir($handle);}echo "<select name=directories";foreach($dir_array as $folder){echo "<option value='".$folder."'>".$folder."</option>}echo "</select>";?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 14, 2006 Author Share Posted December 14, 2006 nice thank you, but where do i enter wich folder to scan for example:themes///Martin :)All that shows now i a blank page!tried to enter it at:[code]$dir = "themes/"; [/code]but didnt work Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 the $dir would be in reference to the current directory the page you are viewing is in. $dir = "./themes";To make it absolute put the absolute path.$dir = "/home/craygo/public_html/themes";Ray Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 14, 2006 Author Share Posted December 14, 2006 Thank you it works like a charm :D Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 14, 2006 Author Share Posted December 14, 2006 hmm , no i didnt get it to workthis is the foldername i have in my rootthemes/scanfolder.php is in the root directory of my wabsiteso this is the code im trying that you gave me:[code]<?php$dir = ".";if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != ".."){ if(filetype($file) == "dir"){ echo "$file<br>"; } } }closedir($handle);}?>[/code]if i use it as it is, it scans root and shows all directories there, however i would like it to scan my themes folder soi added this as you suggested:[b]$dir = "./themes";[/b]also tried[b]$dir = "themes/";[/b]well and every other way i could think of, but it doesnt scan that folder, nothing happens!//Martin* anyone can answer this , would be great;) * Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 15, 2006 Author Share Posted December 15, 2006 Still unresolved, anyone? :) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 15, 2006 Share Posted December 15, 2006 What is the absolute path to your themes directory? Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 15, 2006 Author Share Posted December 15, 2006 well i was hopping not be able to use the absolute path, or if its possible to use a script that check absolute path couse if i want to use this on another server there would be a problem :)//Thanx Quote Link to comment Share on other sites More sharing options...
taith Posted December 15, 2006 Share Posted December 15, 2006 glob() is SOO much faster[code]$files=glob('images/*');echo '<select>';foreach($files as file) echo '<option>'.$file.'</option>';echo '</select>';[/code]you dont need an absolute path either Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 15, 2006 Author Share Posted December 15, 2006 [quote author=taith link=topic=118602.msg485348#msg485348 date=1166180714]glob() is SOO much faster[code]$files=glob('images/*');echo '<select>';foreach($files as file) echo '<option>'.$file.'</option>';echo '</select>';[/code]you dont need an absolute path either[/quote]yes but it only checks what files are in it, not the folders? Quote Link to comment Share on other sites More sharing options...
taith Posted December 15, 2006 Share Posted December 15, 2006 [code]$files = glob('*');foreach($files as $file){ if(strpos(".",$file,1)==false) echo $file;}[/code] Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 16, 2006 Author Share Posted December 16, 2006 [quote author=taith link=topic=118602.msg485389#msg485389 date=1166188545][code]$files = glob('*');foreach($files as $file){ if(strpos(".",$file,1)==false) echo $file;}[/code][/quote]thanx, anyway to make it just scan folders and not the files within?And if its possible just to show the folders in the folder not the whole paththemes/themenamethemename = a foldertheme/ = is the one i dont want to show in drop down menu ;):) Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 16, 2006 Share Posted December 16, 2006 [CODE]<?php$dir = "../themes";$files = scandir($dir);foreach ($files as $file){if(is_dir($file)){echo "$file<br>";}}?>[/CODE] Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 16, 2006 Author Share Posted December 16, 2006 [quote author=JP128 link=topic=118602.msg486012#msg486012 date=1166258160][CODE]<?php$dir = "../themes";$files = scandir($dir);foreach ($files as $file){if(is_dir($file)){echo "$file<br>";}}?>[/CODE][/quote]hmm thank you for your help, but i cant get it to work?scanfolder.php as i called itdoesnt lay in the themes folder its outside it.i tried changing it to this:./themesand /themesbut it doesnt show anything... got any ideas whats wrong? Quote Link to comment Share on other sites More sharing options...
JP128 Posted December 16, 2006 Share Posted December 16, 2006 What is the main directory that scanfolder is in? And what is the main for themes? Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 16, 2006 Author Share Posted December 16, 2006 [quote author=JP128 link=topic=118602.msg486033#msg486033 date=1166262603]What is the main directory that scanfolder is in? And what is the main for themes?[/quote]im in to root directory where scanfolder.php isand that should scan "themes/" folder thats also in root :)//Martin Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted December 16, 2006 Share Posted December 16, 2006 Give this a whizz:[code]<?php$directory = './themes/'; // include trailing slash!if($handle = opendir('./themes')){ while (false !== ($file = readdir($handle))) { if(is_dir($directory . $file)) { echo $file; } } closedir($handle);}?>[/code] Quote Link to comment Share on other sites More sharing options...
ztealmax Posted December 16, 2006 Author Share Posted December 16, 2006 [quote author=heckenschutze link=topic=118602.msg486041#msg486041 date=1166263533]Give this a whizz:[code]<?php$directory = './themes/'; // include trailing slash!if($handle = opendir('./themes')){ while (false !== ($file = readdir($handle))) { if(is_dir($directory . $file)) { echo $file; } } closedir($handle);}?>[/code][/quote]sweet :DTHanx alot , workes like a charm ;)//Martin ;D Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted December 16, 2006 Share Posted December 16, 2006 FYI, a few mistakes the others were making,[code]$dir = "../themes";...foreach ($files as $file){if(is_dir($file)){...[/code]Remember, unless "../themes" is our working directory this will not return "expected" results... Since we're checking if ../themes/%folder% is a folder in ./%folder% ...anyway HTH! Quote Link to comment Share on other sites More sharing options...
Nicklas Posted December 16, 2006 Share Posted December 16, 2006 you can use glob() with the [i]GLOB_ONLYDIR[/i] parameterex[code]<?php$path = "themes"; // path to the dir you want to scanforeach(glob("$path/*", GLOB_ONLYDIR) as $dir) { echo basename($dir) . "<br />\n";}?>[/code] 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.