|
-
Feb 26th, 2006, 02:53 PM
#1
Thread Starter
Frenzied Member
Sort filenames alphabetically
Hello everybody,
Here's my code to scan a particular directory and show all files with a checkbox next to each filename. My client just want to show these filenames sorted alphabetically.
Code:
$dir = "../download/";
$i=0;
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
print "<tr><td height='25'></td></tr>";
while (($file = readdir($dh)) != false) {
// print "filename: $file : filetype: " . filetype($dir . $file) . "\n";
if(filetype($dir . $file)!='dir' and !in_array($file,$att))
{
$i++;
//echo "";
print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
if($_GET[id]=="admin")
print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
else
print"<td class='caption'></td>";
};
}
closedir($dh);
}
}
Thanks.
-
Feb 26th, 2006, 03:01 PM
#2
Re: Sort filenames alphabetically
When you have all the file names in an array, then use the sort function:
http://ch2.php.net/sort
- ØØ -
-
Feb 26th, 2006, 03:30 PM
#3
Thread Starter
Frenzied Member
Re: Sort filenames alphabetically
Can you tell me how to create a dynamic array in PHP as I don't know how many files will be present in folder.
Thanks.
-
Feb 26th, 2006, 03:59 PM
#4
Re: Sort filenames alphabetically
All arrays in PHP are dynamic, just addand remove as and when you require.
-
Feb 26th, 2006, 09:19 PM
#5
Lively Member
Re: Sort filenames alphabetically
lets work with this:
Code:
if(filetype($dir . $file)!='dir' and !in_array($file,$att))
{
$i++;
//echo "";
print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
if($_GET[id]=="admin")
print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
else
print"<td class='caption'></td>";
};
First of all, I am not sure exactly why the last semicolon is there. so I'm going to take it out of mine.
Here's an idea of what to do:
Code:
if(!isdir($file) && !in_array($file,$att))
{
$filearray[] = $file;
}
After that you can display them.
-
Feb 27th, 2006, 04:11 AM
#6
Thread Starter
Frenzied Member
Re: Sort filenames alphabetically
I populated a two dimensional array with filenames and directory names. Its working fine, I checked by printing them on page. But the sort function is not working well. How can I sort a two dimensional string array with respect to first dimension?
Thanks.
-
Feb 27th, 2006, 04:53 AM
#7
Member
Re: Sort filenames alphabetically
Maybe: http://us3.php.net/manual/en/functio...-multisort.php
Which could have been found in NoteMe's link also.
_
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|