Jump to content

List Subdirectories


LemonInflux

Recommended Posts

How do I modificate this code to show subdirectories?

 

<?php
$files = array();
$dir=opendir($f_user);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..' && !is_dir($file))  
{  
	$files[] = $file;  
}  
}  
closedir($dir);
natcasesort($files);
echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo '<li><a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delete.php?del='. $files[$i] .'>Delete</a></li>';
}
echo "</ul>\n";
?>

 

$f_user is the username of the person (The user's directory is named '$f_user'). I just want to know how to list them above the files. I can link them up and sort the rest, it's just that bit. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/68239-list-subdirectories/
Share on other sites

Err, something along the lines of:

 

<?php
$files = array();
$dirs = array();
$dir=opendir($f_user);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..')  
{
 	if(!is_dir($file)){ 
		$files[] = $file;  
	}else{
		$dirs[] = $file;
	}
} 

 

You can then proceed to sort the directories as you do the files, and display those above.

Link to comment
https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343034
Share on other sites

Try:

 

<?php
$files = array();
$dirs = array();
$dir=opendir($f_user);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..')  
{
 	if(!is_dir($f_user.'\\'.$file)){ 
		$files[] = $file;  
	}else{
		$dirs[] = $file;
	}
} 
} 
closedir($dir);
natcasesort($files);
natcasesort($dirs);
foreach($dirs as $v){
array_unshift($files,$v);
}

echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo '<li><a href="'. $f_user .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delete.php?del='. $files[$i] .'>Delete</a></li>';
}
echo "</ul>\n";
?> 

 

I changed what is passed to the is_dir() function to make it absolute, and added everything in the $dirs array to the beginning of your $files array to make things nice and easy

Link to comment
https://forums.phpfreaks.com/topic/68239-list-subdirectories/#findComment-343102
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.