Ordinamento file decrescente

  • Creatore Discussione Creatore Discussione eddie2
  • Data di inizio Data di inizio

eddie2

Nuovo Utente
27 Apr 2020
4
0
1
Salve a tutti, durante questa triste quarantena mi sono avvicinato al mondo del linguaggio php e devo dire che mi sta prendendo parecchio. Essendo all'inizio ovviamente mi intoppo su cose che per voi sono banali, e sicuramente quella che vi chiedo ora lo è senz'altro.
Ho scaricato un pacchetto (Cute File Browser) per la visualizzazione di file e cartelle e l'ho modificato per le mie necessità. In particolare ho trasformato la visualizzazione a blocchi in visualizzazione in linea. Il problema è che non riesco a cambiare l'ordinamento dei file da ascendente a discendente. :rolleyes:
PHP:
<?php


$dir = "Home";


// Run the recursive function


$response = scan($dir);



// This function scans the files folder recursively, and builds a large array


function scan($dir){

  

    $files = array();


    // Is there actually such a folder/file?


    if(file_exists($dir)){

  

        foreach(scandir($dir) as $f) {

      

            if(!$f || $f[0] == '.') {

                continue; // Ignore hidden files

            }


            if(is_dir($dir . '/' . $f)) {


                // The path is a folder


                $files[] = array(

                    "name" => $f,

                    "type" => "folder",

                    "path" => $dir . '/' . $f,

                    "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder

                );

            }

          

            else {


                // It is a file


                $files[] = array(

                    "name" => $f,

                    "type" => "file",

                    "path" => $dir . '/' . $f,

                    "size" => filesize($dir . '/' . $f) // Gets the size of this file

                );

            }

        }

  

    }


    return $files;

}



// Output the directory listing as JSON


header('Content-type: application/json');


echo json_encode(array(

    "name" => "Home",

    "type" => "folder",

    "path" => $dir,

    "items" => $response

));
Ritengo che lo si debba fare dalla pagna scan.php, che contiene il seguente codice. Riuscite a darmi una mano? Grazie. :)
 
Ultima modifica di un moderatore:
@eddie2

Da regolamento del forum, come tutti noi sei tenuto ad usare il tag
PHP (2).png
quando posti del codice php, oppure la funzione codice dalla barra degli strumenti
box inserisci.png

Inoltre IMPORTANTE: Prima di creare una nuova discussione o di rispondere alle discussioni esistenti ricordati di leggere attentamente il Regolamento del Forum e l'eventuale regolamento specifico della sezione!
Grazie
Correggi i tuoi post
mi raccomando altrimenti sarò costretto a cancellare le discussioni
 
  • Like
Reactions: eddie2
Ho scaricato un pacchetto (Cute File Browser) per la visualizzazione di file e cartelle e l'ho modificato per le mie necessità. In particolare ho trasformato la visualizzazione a blocchi in visualizzazione in linea. Il problema è che non riesco a cambiare l'ordinamento dei file da ascendente a discendente. :rolleyes:
 
  • Like
Reactions: eddie2
Inserendo in fondo la funzione usort ottengo l'ordinamento decrescente solo nella pagina principale Home ma non nelle sottocartelle. Cosa mi sfugge? :rolleyes:

Codice:
<?php

$dir = "Home";

// Run the recursive function

$response = scan($dir);


// This function scans the files folder recursively, and builds a large array

function scan($dir){
    
    $files = array();

    // Is there actually such a folder/file?

    if(file_exists($dir)){
    
        foreach(scandir($dir) as $f) {
        
            if(!$f || $f[0] == '.') {
                continue; // Ignore hidden files
            }

            if(is_dir($dir . '/' . $f)) {

                // The path is a folder

                $files[] = array(
                    "name" => $f,
                    "type" => "folder",
                    "path" => $dir . '/' . $f,
                    "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
                );
            }
            
            else {

                // It is a file

                $files[] = array(
                    "name" => $f,
                    "type" => "file",
                    "path" => $dir . '/' . $f,
                    "size" => filesize($dir . '/' . $f) // Gets the size of this file
                );
            }
        }
    
    }
    
    return $files;
}


// Output the directory listing as JSON

header('Content-type: application/json');

usort($response, function($a, $b) {
        if($a['name']==$b['name']) return 0;
        return $a['name'] < $b['name']?1:-1;
});

echo json_encode(array(
    "name" => "Home",
    "type" => "folder",
    "path" => $dir,
    "items" => $response
));
 

Discussioni simili