If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Sort folders/files alphabetically with jQuery

This script works pretty well except that the files list is not ordered. I'd like to sort it alphabetically. Any idea how to do that?

For example, I got this:

0012.jpg
0011.pdf
0024.pdf
0004.jpg
0018.pdf
0008.pdf

While I'd like to get this:

0004.jpg
0008.pdf
0011.pdf
0012.jpg
0018.pdf
0024.pdf

Thanks for any help.

Before sorting the listed files/directories alphabetically, I coverted the function into the way listing the files/directories in list(ul/li) manner instead of the current 'intent' one. I reckon the list way is the most proper way to list nested items. Here is the new function:

if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored

// Just to add spacing to the list, to better
// show the directory tree.

if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...

//Add a class as selector for the jQuery sorting later.
echo "<li>$file<ul class='has-children'>";
getDirectory( "$path/$file");
// Re-call this same function but on a new directory.
// this is what makes function recursive.
echo "</li>";

Hi! I was looking for a code snippet for directory traversal and i found tours (i feel too lazy tonight to do it by myself)
I tested on linux and works ok, on windows ok except for long paths.

And I actually need to index some dirs and files that have a really deep dir structure.
It just freezes when the path is longer than 255...
Do you have any ideas on how i can traverse such a structure?
Thank you very much!
Any help is really appreciated!

To be honest, I don't really like the supressing of potentional errors( @opendir ).
I was actually creating an online file management system, and already had this coded. (Currently secured it by IP Access).

PHP Code:

<?php

if($_SERVER['REMOTE_ADDR'] != 'YOUR_IP') {

die("Only the webmaster can access this page's content. Please go back and open another file.");

Php way to do the sorting

Originally Posted by freemany

Before sorting the listed files/directories alphabetically, I coverted the function into the way listing the files/directories in list(ul/li) manner instead of the current 'intent' one. I reckon the list way is the most proper way to list nested items. Here is the new function:

if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored

// Just to add spacing to the list, to better
// show the directory tree.

if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...

//Add a class as selector for the jQuery sorting later.
echo "<li>$file<ul class='has-children'>";
getDirectory( "$path/$file");
// Re-call this same function but on a new directory.
// this is what makes function recursive.
echo "</li>";

hello,
well for me this function make the error :Warning: readdir() expects parameter 1 to be resource, boolean given
for
while (false !== ($file = readdir($dh))) {

any idea?

I must say I don't think it's a good practice to put html in your code, it's better to separate front presentation from back, so this function will be more logic to integrate to make others operations (of course it's not a big deal to modify)

dir to array recursive

The functions on this site did not work for me.
I wrote one myself.
It converts the directory structure into an array.
It returns some relevant data in the {data} key and loops through the rest.
It might not be the cleanest script but it does the job for me.
Maybe it helps someone.