Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> How To Sort Files Of A Directory using Php
itssami
post May 1 2006, 11:30 AM
Post #1


Super Member
*********

Group: Members
Posts: 258
Joined: 13-November 05
Member No.: 14,234



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.

CODE
<?
$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 [code][/code] tags for code!


This post has been edited by electriic ink: May 1 2006, 12:07 PM
Go to the top of the page
 
+Quote Post
mole2k9
post May 1 2006, 12:34 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 21
Joined: 30-April 06
Member No.: 22,816



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

CODE
<?
$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);
?>


This post has been edited by mole2k9: May 1 2006, 12:36 PM
Go to the top of the page
 
+Quote Post
electriic ink
post May 1 2006, 12:37 PM
Post #3


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,216
Joined: 11-February 05
From: Heaven
Member No.: 3,709



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

CODE
<?
$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
Go to the top of the page
 
+Quote Post
jlhaslip
post May 1 2006, 01:26 PM
Post #4


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,970
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
mole2k9
post May 1 2006, 03:10 PM
Post #5


Newbie [Level 1]
*

Group: Members
Posts: 21
Joined: 30-April 06
Member No.: 22,816



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,

CODE
<?
$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.

This post has been edited by mole2k9: May 1 2006, 03:08 PM
Go to the top of the page
 
+Quote Post
jlhaslip
post May 1 2006, 03:11 PM
Post #6


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,970
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
itssami
post May 1 2006, 03:31 PM
Post #7


Super Member
*********

Group: Members
Posts: 258
Joined: 13-November 05
Member No.: 14,234



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

QUOTE(mole2k9 @ May 1 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,

CODE
<?
$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.

Go to the top of the page
 
+Quote Post
jlhaslip
post May 1 2006, 03:49 PM
Post #8


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,970
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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

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

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

Go to the top of the page
 
+Quote Post
mole2k9