Jump to content
xisto Community
itssami

How To Sort Files Of A Directory using Php

Recommended Posts

Wondering if any one can help me out.
I can use the following script to display the files and folders but only the files are in alphabetical and not the folders, I know it will be using another array but cant seem to get it to work.
If you want a link to the sample page just ask me to send an email, Im sure the forum rules were i cant post a link to my site, but if im told its ok i will.

Im posting here because i used a little script from this Post to help with ording the files.

I attached a screen shot to show what im doing .

Thanks

<?phpfunction ListFolder($path){$narray=array();     //using the opendir function    $dir_handle = @opendir($path) or die("Unable to open $path");        //Leave only the lastest folder name    $dirname = end(explode("/", $path));$i=0;        //display the target folder.    echo ("<li><img alt='PDF' src='../images/Folder.png'> $dirname\n");    echo "<ul>\n";    while (false !== ($file = readdir($dir_handle)))     {   if($file!="." && $file!="..")        {            if (is_dir($path."/".$file))            {                //Display a list of sub folders.                ListFolder($path."/".$file);            }            else            {                //Display a list of files.//                echo "<img alt='PDF' src='../images/PDF.png'> $file<br/>";                $narray[$i]=$file;                 $i++;            }                    }   }    //closing the directory    closedir($dir_handle);sort($narray);  for($i=0; $i<sizeof($narray); $i++) { echo "<img alt='PDF' src='../images/PDF.png'> $narray[$i]<br/>";}     echo "</ul>\n";    echo "</li>\n";}?>

Untitled-1.pdf

Edited by jibberweed (see edit history)

Share this post


Link to post
Share on other sites

I got it im giving complete code to allow even expandable file structure hope this helps someone besides my self.
as note i changed quite a bit.

if you want the java files just ask and i will send them to you

<?php/* Rendering */ function list_dir($path) { $items = get_sorted_entries($path); if (!$items) return; foreach($items as $item) { if ($item->type=='dir') {     echo ("<li><img alt='PDF' src='../../../images/Folder.png'>".$item->entry."\n");    echo "<ul>\n";list_dir($item->full_path); } else { echo "<img alt='PDF' src='../../../images/PDF.png'> ".$item->entry."<br/>";} } echo "</ul>"; echo "</li>\n";} /* Finding */ function get_sorted_entries($path) { $dir_handle = @opendir($path) ; $items = array(); while (false !== ($item = readdir($dir_handle))) { $dir =$path.'/'.$item; if ( $item == '.' || $item =='..' ) continue; if(is_dir($dir)) { $items[] = (object) array('type'=>'dir','entry'=>$item, 'full_path'=>$dir); } else { $items[] = (object) array('type'=>'file','entry'=>$item, 'full_path'=>$dir); } } closedir($dir_handle); usort($items,'_sort_entries'); return $items; } /* Sorting */ function _sort_entries($a, $ { return strcmp($a->entry,$b->entry); }?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://forums.xisto.com/no_longer_exists/ xmlns="http://forums.xisto.com/no_longer_exists/;	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>	<title>TITLE</title>		<link rel="stylesheet" href="../jquery.treeview.css" />	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>	<script src="../lib/jquery.cookie.js" type="text/javascript"></script>	<script src="../jquery.treeview.js" type="text/javascript"></script>		<script type="text/javascript" src="demo.js"></script>		</head>	<body>		<div id="treecontrol">		<a title="Collapse the entire tree below" href="#"> Collapse All</a> | 		<a title="Expand the entire tree below" href="#"> Expand All</a> | 		<a title="Toggle the tree below, opening closed branches, closing open branches" href="#">		Toggle All</a>	</div><ul id="red" class="treeview-red"><?php list_dir("directory you want to search"); ?>	</ul><hr /> </body></html>

Untitled-1.pdf

Edited by jibberweed (see edit history)

Share this post


Link to post
Share on other sites

The following code displays the files of folder...but they are displaced by the order of adding...i want to sord the files / folders alphabetically and sord by accending order and by decending order..
can some one help me.

<?$path = "";$dir_handle = @opendir($path) or die("Unable to open $path");echo "Directory Listing of $path<br/>";while($file = readdir($dir_handle)) {	if(is_dir($file)) {		continue;		 }   else if($file != '.' && $file != '..') {			  echo "<a href='$path/$file'>$file</a><br/>";			}}//closing the directoryclosedir($dir_handle);?>

Notice from electriic ink:
Use tags for code!



>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

this is how to read folders and sub-folders and display them aphabetically

treeview(); // first.... call this function, change the parameter to change path (parameter is optional)




function treeview($path='.')
{
echo "<ul>";
recursive_read($path); //call recursion method to display folders and subfolders until it reach the bottom
echo "</ul>";
}

function recursive_read($path)
{
// open the path from parameter $path
$handle = opendir($path);

while($currentFile = readdir($handle) ){ // assign file names to $fileArray array
$fileArray[] = $currentFile;
}
closedir($handle); // close the handle;
asort($fileArray); // sort the folder alphabetically

// display main folders from directory using loop
foreach($fileArray as $file):
if($file != "." && $file != ".."){

$file = $path .'/'. $file;
$exp = explode('/', $file);
$file_name = end($exp);

if(is_dir($file)){ // check if the file is folder
echo "<li><span class='folder'>".$file_name."</span>";
if(!isEmptyDir($file)){
echo "<ul>";
recursive_read($file);
echo "</ul>";
}
echo "</li>";
}else{
// uncomment this to display non folder files
echo "<li><span class='file'>".$file_name."</span></li>";
}

}
endforeach;
}

function isEmptyDir($file){
return (($files = @scandir($file)) && count($files) <= 2);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.