Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]DirectoryIterator sortowanie wyniku
!*!
post
Post #1





Grupa: Zarejestrowani
Postów: 4 298
Pomógł: 447
Dołączył: 16.11.2006

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


Tym sposobem odczytuję listę plików w katalogu, pomijając . oraz .. :
  1. $directory = 'folder';
  2.  
  3. foreach (new DirectoryIterator($directory) as $files) {
  4.  
  5. $onlydirectory = !$files -> isDot() and !$files -> isDir() and !$files -> isExecutable();
  6. $filename = $files -> getFilename();
  7. $date = $files -> getMTime();
  8. $date = date("d-m-Y H:i", $date);
  9.  
  10. $size = $files -> getSize();
  11. $sizeinkb = round($size/1024);
  12.  
  13. if($onlydirectory) {
  14.  
  15.  
  16. echo '<tr class="'. $class.'"><td class="tdf"><input type="checkbox" /></td><td class="tdf">'.imagefile($filename).'</td><td class="tdk">'.$filename.'</td><td class="tdn">'.$size.'</td><td class="tdn">'.$date.'</td><td class="tdu">edycja</td><td class="tdu">usun</td></tr>';
  17.  
  18.  
  19. }
  20.  
  21. }


Chciałbym nazwy plików posortować alfabetycznie:

  1. $directory = 'folder';
  2.  
  3. foreach (new DirectoryIterator($directory) as $files) {
  4.  
  5. $onlydirectory = !$files -> isDot() and !$files -> isDir() and !$files -> isExecutable();
  6. $filename = $files -> getFilename();
  7. $date = $files -> getMTime();
  8. $date = date("d-m-Y H:i", $date);
  9.  
  10. $size = $files -> getSize();
  11. $sizeinkb = round($size/1024);
  12.  
  13. if($onlydirectory) {
  14.  
  15.  
  16. $filetab[] = $filename;
  17.  
  18.  
  19. }
  20.  
  21. }
  22. sort($filetab);
  23.  
  24.  
  25. foreach ($filetab as $filename) {
  26. echo '<tr class="'. $class.'"><td class="tdf"><input type="checkbox" /></td><td class="tdf">'.imagefile($filename).'</td><td class="tdk">'.$filename.'</td><td class="tdn">'.$sizeinkb.'</td><td class="tdn">'.$date.'</td><td class="tdu">edycja</td><td class="tdu">usun</td></tr>';
  27.  
  28. }


Jednak nie wiem czy robię to dobrze, oraz jak odczytać w foreach inne dane?

Kod
foreach ($filetab as $filename && )
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


  1. <?php
  2. // brakujace zmienne
  3. $class = "NIC";
  4.  
  5.  
  6. $directory = '.';
  7. $filetab = array();
  8.  
  9.  
  10. foreach (new DirectoryIterator($directory) as $files)
  11. {
  12.  
  13. $onlydirectory = !$files -> isDot() and !$files -> isDir() and !$files -> isExecutable();
  14. $filename = $files -> getFilename();
  15. $date = $files -> getMTime();
  16. $date = date("d-m-Y H:i", $date);
  17.  
  18. $size = $files -> getSize();
  19. $sizeinkb = round($size/1024);
  20.  
  21. if($onlydirectory)
  22. {
  23. // $files jest obiektem iteratora, z kazdym wywolaniem w foreach zmienia wartosci
  24. // wewnatrz siebie, wiec nie mozna go przechowywac, nalezy skopiowac z niego nazwe, wielkosc, itp
  25. $filetab[$filename] = array( 'filename' => $files->getFileName(), 'size' => $files->getSize(), 'mtime' => $files->getMTime());
  26. }
  27.  
  28. }
  29. ksort($filetab);
  30.  
  31. foreach ($filetab as $filename => $files) {
  32. printf( "Filename: %s, Size: %.2fKB, Modified at: %s<br />", $files['filename'], $files['size']/1024, date('d-m-Y H:i', $files['mtime']));
  33. //print_r($files);
  34. }


Zapomnialem o malym szczegole zwiazanym z interatorem -> patrz komentarz.
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: 27.12.2025 - 23:21