tags:

views:

40

answers:

3

hey guys, i'm connecting to a ftp-server and displaying all items in a list. i want that list to be ordered alphabetically.

shouldn't that do it?

        // get contents of the current directory
    $contents = ftp_nlist($conn_id, $path);
    sort($contents);

that's a part of the script!

// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
sort($contents);
print "<ul class='server_list'>";
foreach ($contents as $value) {
    $line = str_replace($path, "", $value);
    $ext = pathinfo($line, PATHINFO_EXTENSION);
    if (strlen($ext) > 0) { //File
        print "<li class='file'>";
        print "<a href='d.php/?p=". $path . $line."'>$line</a>";
        print "</li>";
    } else { //Folder
        print "<li class='folder'>";
        print "<a href='?p=". $path . $line."'>$line</a>";
        print "</li>";
    }
}
print "</ul>";

moreover i'd like to have all folders at the top. so all folders should be ordered alphabetically and afterwards all files should be listed in abc...

print_r($contents) before the sorting gives me:

Array ( 
    [0] => /userupload/OrderNo_100750_HT 
    [1] => /userupload/README.txt 
    [2] => /userupload/anotherfolder 
    [3] => /userupload/avatar.jpg 
    [4] => /userupload/subfolder1 
) 
+1  A: 

You are right, for a reason it does not seems to have any effect.

The reason is simple: The sort is case-sensitive. And considering this, the array is already sorted (lower case letters come after capital letters).

If you want to have a case-insensitive sort, you can use natcasesort():

natcasesort($contents);
print_r($contents);

gives

Array
(
    [2] => /userupload/anotherfolder
    [3] => /userupload/avatar.jpg
    [0] => /userupload/OrderNo_100750_HT
    [1] => /userupload/README.txt
    [4] => /userupload/subfolder1
)
Felix Kling
A: 

For a start, natcasesort will sort in "natural order", case-insensitive.

To sort folders to the top you would need to ask the FTP server if something is a folder, then put it into a separate array and merge both arrays back together afterwards. It would appear that using ftp_rawlist and parsing the result would be the best way to do that.

deceze
A: 

You will need two Arrays. One with the directories and one with the files. Then sort each.

The problem is, you do not have a function in PHP to test weather a file is a directory, or file.

Here is the workaround, that could be found at php.net:

function ftp_is_dir($dir, $ftpcon) {
    // get current directory
    $original_directory = ftp_pwd( $ftpcon );
    // test if you can change directory to $dir
    // suppress errors in case $dir is not a file or not a directory
    if ( @ftp_chdir( $ftpcon, $dir ) ) {
        // If it is a directory, then change the directory back to the original directory
        ftp_chdir( $ftpcon, $original_directory );
        return true;
    } 
    else {
        return false;
    }        
} 

With this you can do:

// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
$dirs = array();
$files = array();
foreach ($contents as $value) {
  if (ftp_is_dir($value, $conn_id)) $dirs[] = $value;
  else $files[] = value;
}
$contents = array();
sort($dirs);
sort($files);
$contents = array_merge ($dirs, $files);
...
JochenJung
$ftpcon is $conn_id in your case.
JochenJung
I'd prefer a version without `global`.
deceze
Ok, I just changed it to work without global
JochenJung
Good, thank you. :)
deceze