I had some issues while trying to sort my opendir. The code is basic opendir and is set to display a urls directory. I was trying to sort the output as either folder type or by alphabet but I had no luck. Here is the code:
<?
$dir = "../";
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "<a href=''>".$file."<a><br>";
}
}
}
?>
How can I sort my $file
as either alphabet or folder type?
You might start with something like the following script. I've added comments to explain it.
For sorting things, change the while-loop to not output directly, but to store dir or file in an array by type. Then return that array, let's name it dircontent. Then apply your sort() function to dircontent. Then foreach over dircontent to output the sorted files and folders (you saved the type before, so you know again, if its file or folder).
If you want this to go deeper in the hierarchie, put a showDir($dir) inside the is_dir() check.
<?php
/**
* List the folders of a dir and show only PHP files.
*/
function showDir($dir)
{
$handle = opendir($dir);
while ($dir = readdir($handle)) {
// exclude dot files/folders
if ($dir === '.' or $dir === '..') {
continue;
}
// is this a dir?
if(is_dir($dir)) {
echo '<a href=' . $dir . '>' . $dir . '<a><br>';
}
// is it a file?
if(is_file($dir)) {
// get file extension, in order to check if it's a PHP file
$ext = pathinfo($dir, PATHINFO_EXTENSION);
// is it a PHP file?
if($ext === 'php') {
// indent files a bit
echo '|- ' . $dir . '<br>';
}
}
}
closedir($handle);
}
showDir(".");
?>
Keep in mind, that there are other solutions around: to name a few scandir(), glob() or DirectoryIterator(). The opendir()/readdir()/closedir() approach is a bit rusty, but works.
Sort by filename (similar to your code, but with exclusions, and instead of echoing, the files are put in an array which is sorted and echoed later):
<ul>
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != 'index.php') {
$thelist[] = $file;
}
}
closedir($handle);
}
sort($thelist);
foreach($thelist as $file) {
echo '<li><a href="'.$file.'">'.$file.'</a></li>';
}
?>
</ul>
To sort by file type, you'd need something slightly more complex:
<ul>
<?php
if ($handle = opendir('.')) {
$thelist = array();
while (false !== ($file = readdir($handle))) {
//Don't show ., .., or any php files
if ($file != "." && $file != ".." && substr($file, -3) != 'php') {
if(is_dir($file)) {
//Add directories to their own filetype that will appear at the beginning
$thelist['aaadir'][] = $file;
} else {
//Add each file to an array based on its filetype
$ext = pathinfo($file, PATHINFO_EXTENSION);
$thelist[$ext][] = $file;
}
}
}
closedir($handle);
}
//Sort the arrays alphabetically by filetype
ksort($thelist);
foreach($thelist as $filetype) {
//sort this list of files (in a specific filetype) alphabetically
sort($filetype);
foreach($filetype as $file) {
echo '<li><a href="'.$file.'">'.$file.'</a></li>';
}
}
?>
</ul>