Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How To Sort Files Of A Directory using Php


17 replies to this topic

#1 itssami

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 258 posts

Posted 01 May 2006 - 11:30 AM

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 directory
closedir($dir_handle);

?>

Notice from electriic ink:
Use
 tags for code!

Edited by electriic ink, 01 May 2006 - 12:07 PM.


#2 mole2k9

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 21 posts

Posted 01 May 2006 - 12:34 PM

haven't tested this but give it a try should work, create an array of the file names and then use the sort function.

<?
$path = "";
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
$i=0;
while($file = readdir($dir_handle))
{
	if(is_dir($file))
	{
		continue;
	}
	else if($file != '.' && $file != '..')
	{
		//echo "<a href='$path/$file'>$file</a><br/>";
		$narray[$i]=$file;
		$i++;
	}
}
sort($narray);
for($i=0;i<sizeof($narray);$i++)
{
	echo "<a href=".chr(34).$path."\".$narray[$i].chr(34).">".$file."</a><br/>";
}
//closing the directory
closedir($dir_handle);
?> 

Edited by mole2k9, 01 May 2006 - 12:36 PM.


#3 electriic ink

    "Britons never never shall be slaves." As true now as it was in 1740.

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,262 posts
  • Gender:Male
  • Location:Heaven
  • Interests:Promotion: Aug 4 2005 8.24pm BST
  • myCENT:74.43

Posted 01 May 2006 - 12:37 PM

So you want to display the files/folders in a directory alphabetically? The code is as follows:

<?
$path = "";
$files_gathered = array();

$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 != '..') {
   $files_gathered[] = $file;
  }

}

//closing the directory
closedir($dir_handle);

// begin sorting and displaying the files

 $files_gathered = sort ($files_gathered);
 $count = count ($files_gathered);

 for ($loop_start = 0; isset($files_gathered[$loop_start]); $loopstart++) {
  echo "<a href='" . $path . $files_gathered[$loop_start] . 
"' title="' . $files_gathered[$loop_start] . "'> " . $files_gathered[$loop_start] . " </a> <br />";
 } 
 
?>

I haven't tested it yet but it should still work

#4 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 01 May 2006 - 01:26 PM

Both of those scripts look like they will work to sort in ascending order.
To sort the array in descending order, use the function rsort() instead of sort(). Everything else should remain the same.

#5 mole2k9

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 21 posts

Posted 01 May 2006 - 03:10 PM

Opps mean to use rsort also made 1 other change , chnage$sfile to $narray[$i] in,


echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";


this is tested and works,

<?
$path = "";
$narray=array();
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
$i=0;
while($file = readdir($dir_handle))
{
	if(is_dir($file))
	{
		continue;
	}
	else if($file != '.' && $file != '..')
	{
		//echo "<a href='$path/$file'>$file</a><br/>";
		$narray[$i]=$file;
		$i++;
	}
}
rsort($narray);

for($i=0; $i<sizeof($narray); $i++)
{
echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";

}

//closing the directory
closedir($dir_handle);
?>

you might want to add a function to check whether the path has a \ at the end and add one if not.

Edited by mole2k9, 01 May 2006 - 03:08 PM.


#6 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 01 May 2006 - 03:11 PM

All it needs now is a form to select the directory and a radio button set to select ascending or descending sort order. Then an If statement to sort in the chosen order. And a set of html ul tags and li tags. And a class to hook the styling to for the html / css and you are all set.

I prefer a single page script / form method personally.

Looks good.

#7 itssami

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 258 posts

Posted 01 May 2006 - 03:31 PM

i dont mean to bother but it still shows blank screen... for example i have a folder "test" in htdocs.. and i gave the path
 $path = "test";
but it shows blank screen.. and if i give any wrong name of the folder which doesnt even exists in htdocs , it still shows blank page, even it should say "Unable to open ..."
im trying to find the problem but cant..

View Postmole2k9, on May 1 2006, 03:10 PM, said:

Opps mean to use rsort also made 1 other change , chnage$sfile to $narray[$i] in,
echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";
this is tested and works,

<?
$path = "";
$narray=array();
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
$i=0;
while($file = readdir($dir_handle))
{
	if(is_dir($file))
	{
		continue;
	}
	else if($file != '.' && $file != '..')
	{
		//echo "<a href='$path/$file'>$file</a><br/>";
		$narray[$i]=$file;
		$i++;
	}
}
rsort($narray);

for($i=0; $i<sizeof($narray); $i++)
{
echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";

}

//closing the directory
closedir($dir_handle);
?>

you might want to add a function to check whether the path has a \ at the end and add one if not.


#8 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 01 May 2006 - 03:49 PM

Have a look at the script I sent to you and you will notice there is a line in in which sets the path to "./". This references the path which the script is run from. Modify this path to include the name of the directory on a relative basis.
$path = "./test/"

Also, if you are making changes to the script and want further advise, I would reccomend including a copy of the actual code which is not working. The sample you list above does not have any directory in it so I am curious about whether you actually have a valid path in the source code.

Also, the echo statement is commented out in the listing you display.
		//echo "<a href='$path/$file'>$file</a><br/>";
Check to make sure the commenting slashes are removed before running the script. With these slashes in place, the information is not being sent to the Browser. Might explain the blank page.

#9 mole2k9

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 21 posts

Posted 01 May 2006 - 04:14 PM

try $path = "/test";

#10 itssami

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 258 posts

Posted 01 May 2006 - 08:31 PM

thanks..i have edited a lil bit the above code..now it displays all the files and folders within a directory...but the problem is that , for example i have index.php file in that directory , it shows that index.php file also in the list..i want that it should show every file but NOT index.php.. what i should do for that ??

<?php
$path = "./";
$narray=array();
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
$i=0;
while($file = readdir($dir_handle))
{
	 if($file != '.' && $file != '..')
	{
		//echo "<a href='$path/$file'>$file</a><br/>";
		$narray[$i]=$file;
		$i++;
	}
}
sort($narray);

for($i=0; $i<sizeof($narray); $i++)
{
echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";

}

//closing the directory
closedir($dir_handle);
?>

Edited by itssami, 01 May 2006 - 08:31 PM.





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users