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