Jump to content

scan a folder and get names of all folders within that folder?


ztealmax

Recommended Posts

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
Link to comment
Share on other sites

hmm , no i didnt get it to work
this is the foldername i have in my root
themes/

scanfolder.php is in the root directory of my wabsite

so 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 so
i 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;) *
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

[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 path
themes/themename

themename = a folder
theme/ = is the one i dont want to show in drop down menu ;)
:)
Link to comment
Share on other sites

[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 it
doesnt lay in the themes folder its outside it.

i tried changing it to this:
./themes
and
/themes

but it doesnt show anything... got any ideas whats wrong?
Link to comment
Share on other sites

[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 is
and that should scan "themes/" folder thats also in root :)

//Martin
Link to comment
Share on other sites

[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 :D

THanx alot , workes like a charm ;)

//Martin

;D
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.