Hi, I am trying to list directories and files within a directory.
so if
i were to want to list all files and directores of the "templates" folder.
i'd want to show like so.
File:file1.ext
Directory:templateName
filename1.ext
filename2.ext
filename3.ext
filename4.ext
filename5.ext
filename6.ext
Directory:templateName2
filename1.ext
filename2.ext
filename3.ext
filename4.ext
filename5.ext
filename6.ext
File:file2.ext
Basically i want to list all files and subdirectories and files within subdirectories within the directory.
all i have been able to do is list only the main folder...basic stuff.
i still new to this part of PHP.
I'd show you my current code but... its kinda destroyed to the point where i had to start from scratch and thats why im asking for help.
Please help.
EDIT
Well i figured it out.. i think....
this is my solution
$mdir = 'templates';
//--Start Extraction Check between Files and Dir's
if ($handle = opendir($mdir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
//Is Directory List Content Of dir
if(is_dir($mdir.'/'.$file)){
echo "<strong>Dir:$file</strong><br>\n";
if(is_dir($mdir.'/'.$file)){
//-----------------
if ($handle1 = opendir($mdir.'/'.$file)) {
while (false !== ($file2 = readdir($handle1))) {
if ($file2 != "." && $file2 != "..") {
echo "<em>$file2</em><br>\n";
}
}
closedir($handle1);
}
//-----------------
}
}
else{
//Is File Do Nothing.Just List.
if(is_file($mdir.'/'.$file)){
echo "File:$file<br>\n";
}
}
}
}
closedir($handle);
}
Now i just need to figure out how to list the Directories FIRST and the Files in the main dir later... or Listing them separately... as in Subdirectores at the top.. then Files at bottom.
if i come up with a solution using the code above. i'll post back.
if you ppl have a solution already. im willing and more then HAPPY to hear.
Edit... Well i guess i didnt look hard enough... It wont do subdirectores of subdirectories.. which is my main goal now..
i have this code
$mdir = 'templates';
//--Start Extraction Check between Files and Dir's
if ($handle = opendir($mdir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file !="cdpanel-install.php") {
//Is Directory List Content Of dir
if(is_dir($mdir.'/'.$file)){
echo "<strong>Dir:$file</strong><br>\n";
if(is_dir($mdir.'/'.$file)){
//-----------------
if ($handle1 = opendir($mdir.'/'.$file)) {
while (false !== ($file2 = readdir($handle1))) {
if ($file2 != "." && $file2 != "..") {
echo "<em>$file2</em><br>\n";
}
}
closedir($handle1);
}
//-----------------
}
}
}
}
closedir($handle);
}
//List Files ONLY
print('<hr>');
if ($handle = opendir($mdir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file !="cdpanel-install.php") {
//Is NOT Directory List Content Of dir
//-----------------
if(is_file($mdir.'/'.$file)){
echo "File:$file<br>\n";
}
}
}
closedir($handle);
}
Now this will list the main dir files separatly from the dir's. so that the files that arent in a subdirectory get confused with a subdirectory file and wont be listed between dirs, cluttering them up.
Again help much apperciated.