Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][HTML]Sortowanie po dacie przez asort ? czy inaczej ?, - prosze o pomoc w rozwiązaniu problemu
Luki2604
post
Post #1





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 17.01.2007

Ostrzeżenie: (0%)
-----


witam,
mam skrypt, który pobiera liste plików z folderów i wyświetla jako NAZWA PLIKU - DATA MODYFIKACJI ( poprzez filemtime) - USUŃ.
Skrypt działa ok, ale chciałbym aby wyświetlał pliki według daty modyfikacji - od najmłodszego do najstarszego.
Próbuję z różnymi funkcjami, tj. asort, ksort, ale mi sie nie udaje :/
Proszę o pomoc, bo już mi ręce opadają :/
Pozdrawiam, Lukasz.
  1. <?php
  2. function dirList ($directory)
  3. {
  4.  
  5.    // create an array to hold directory list
  6.    $results = array();
  7.  
  8.  
  9.    // create a handler for the directory
  10.    $handler = opendir($directory);
  11.  
  12.    // keep going until all files in directory have been read
  13.    while ($file = readdir($handler)) {
  14.  
  15.        // if $file isn't this directory or its parent,
  16.        // add it to the results array
  17.  
  18.        if ($file != '.' && $file != '..' && is_file($directory."/".$file) == TRUE)
  19.            $results[] = $file;
  20.    }
  21.  
  22.    // tidy up: close the handler
  23.    closedir($handler);
  24.  
  25.    // done!
  26.    return $results;
  27. }
  28.  
  29. header('Content-type: text/html; charset=utf-8'); //musi byc bo rozne serwery sa z roznym kodowaniem
  30.  
  31. //<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  32. $html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
  33. $html .= '<html>';
  34. $html .= '<head>';
  35. $html .= '<meta http-equiv="refresh" content="3;url=http: ..." />';
  36. $html .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  37. $html .= '<title>';
  38. $html .= "Pliki";
  39. $html .= '</title>';
  40. $html .= '</head>';
  41. $html .= '<body BACKGROUND = "111.jpg">';
  42. $html .= '<FONT FACE="arial">';
  43.  
  44. $html .= "<div>";
  45.  
  46. $aDir = "./myfiles";
  47.  
  48. $files = dirList($aDir);
  49.  
  50. if (TRUE == array_key_exists("deleteFile", $_GET))
  51.  {
  52.  $file = $aDir."/".$_GET["deleteFile"];
  53.  if ($file == $_SERVER["SCRIPT_NAME"])
  54.    {
  55.    $html .= "Nie mozna usunac tego pliku<br>";
  56.    }
  57.    else if (file_exists($file) && unlink($file))
  58.    {
  59.    $html .= "Usunieto $file<br>";
  60.    }
  61.  else
  62.    {
  63.    $html .= "Blad podczas usuwania pliku $file<br>";
  64.    }
  65.  }
  66.  
  67. foreach ($files as $val)
  68.  {
  69.      $html .= $val .' Odnotowano: '. date('H:i:s. d-F-Y.', filemtime($aDir."/".$val))."</a><hr><br>";
  70.       }
  71.  
  72. $html .= "</div>";
  73. $html .= '</body>';
  74. $html .= '</html>';
  75. $html .= '</font>';
  76.  
  77. echo $html;
  78. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
erix
post
Post #2





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Po prostu dodawaj datę modyfikacji przy wczytywaniu:
  1. <?php
  2. $results[] = $file;
  3. ?>

I wciśnij ją w klucz tablicy dla danego pliku.

Potem sobie poradzisz z sortowaniem. [;

edit - faktycznie - kilka plików może mieć ten sam czas, powinno być na odwrót
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 02:08