Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Wyświetlanie listy katalogów i plików
Forum PHP.pl > Forum > Przedszkole
Kshyhoo
Spłodziłem po wielkich skrypt mękach skrypt galerii, gdzie można "chodzić" po katalogach. Wszystko pięknie działa na localhost... ale po wrzuceniu na serwer, nie ma alfabetycznego sortowani a plików. Siedzę już and tym kilka dni, i nie mogę nic sensownego wymyśleć, wciąż błędy:
Cytat
Warning: ksort() expects parameter 1 to be array, string given in...

Proszę o pomoc, oto kod:
  1. <?php
  2. function ListaKatalogow($path) {
  3. $base_dir = $_SERVER['DOCUMENT_ROOT']; // wszystkie ścieżki w stosunku do serwera
  4. $file = scandir($base_dir."/".$path);
  5. foreach($file as $files) {
  6. if (($files != ".") && ($files != "..")) {
  7. chdir($base_dir."/".$path);
  8. if (is_dir($files)) {
  9. if (substr($path, strlen($path) - 3, 3) == 'img') {
  10. echo '<a class="folder1" href="cpax.php?go=galeria&path='.$path.'/'.$files.'">'.$files.'</a>';
  11. } else {
  12. echo '<a class="folder2" href="cpax.php?go=galeria&path='.$path.'/'.$files.'">'.$files.'</a>';
  13. }
  14. }
  15. }
  16. }
  17. if ($dir_handle = opendir($base_dir."/".$path)) {
  18. while ($file = readdir($dir_handle)) {
  19. chdir($base_dir."/".$path);
  20. if (($file != ".") && ($file != "..")) {
  21.  
  22. ksort($file);
  23. if (is_file($file)) {
  24.  
  25. $list = $_SERVER['SERVER_NAME'];
  26. $lvl1 = $_SESSION['tree']['lvl1'];
  27. $lvl2 = $_SESSION['tree']['lvl2'];
  28. $photo = getimagesize($file);
  29. if($photo[0]>160) {
  30. $szer = '160';
  31. } else {
  32. $szer = 'auto';
  33. }
  34.  
  35. /* inne pliki niż graficzne */
  36. if (substr($file, strlen($file) - 4, 4) == 'html') {
  37. echo '';
  38. } elseif (substr($file, strlen($file) - 3, 3) == 'rar') {
  39. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" width="150" height="150" src="./gfx/p_rar.png" /></a><br />'.$file.'<br /><a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"> &nbsp; <img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  40. } elseif (substr($file, strlen($file) - 3, 3) == 'zip') {
  41. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" width="150" height="150" src="./gfx/p_zip.png" /></a><br />'.$file.'<br /><a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"> &nbsp; <img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  42. } elseif(substr($file, strlen($file) - 3, 3) == 'psd') {
  43. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" width="150" height="150" src="./gfx/p_psd.png" /></a><br />'.$file.'<br /><a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"> &nbsp; <img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  44. } elseif(substr($file, strlen($file) - 3, 3) == 'pdf') {
  45. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" width="150" height="150" src="./gfx/p_pdf.png" /></a><br />'.$file.'<br /><a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"> &nbsp; <img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  46. } elseif(substr($file, strlen($file) - 3, 3) == 'avi') {
  47. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" width="150" height="150" src="./gfx/p_avi.png" /></a><br />'.$file.'<br /><a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"> &nbsp; <img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  48. /* pliki graficzne */
  49. } elseif(substr($file, strlen($file) - 3, 3) == 'jpg') {
  50. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" src="http://'.$list.'/'.$path.'/'.$file.'" alt='.$file.' width='.$szer.' /></a><br />'.$file.'<br />'.$photo[0].' x '.$photo[1].' &nbsp; <a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"><img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  51. } elseif(substr($file, strlen($file) - 4, 4) == 'jpeg') {
  52. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" src="http://'.$list.'/'.$path.'/'.$file.'" alt='.$file.' width='.$szer.' /></a><br />'.$file.'<br />'.$photo[0].' x '.$photo[1].' &nbsp; <a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"><img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  53. } elseif(substr($file, strlen($file) - 3, 3) == 'gif') {
  54. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" src="http://'.$list.'/'.$path.'/'.$file.'" alt='.$file.' width='.$szer.' /></a><br />'.$file.'<br />'.$photo[0].' x '.$photo[1].' &nbsp; <a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"><img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  55. } elseif(substr($file, strlen($file) - 3, 3) == 'png') {
  56. echo '<div class="przeglad"><a href="http://'.$list.'/'.$path.'/'.$file.'"><img border="0" src="http://'.$list.'/'.$path.'/'.$file.'" alt='.$file.' width='.$szer.' /></a><br />'.$file.'<br />'.$photo[0].' x '.$photo[1].' &nbsp; <a href="cpax.php?go=galeria&amp;akcja=usun&amp;link='.$lvl1.'/'.$lvl2.'&amp;plik='.$file.'"><img onclick="_delete(event,this);" border="0" src="./gfx/icon-recycle.gif" alt="x" title="Usuń" /></a></div>';
  57.  
  58. } else {
  59. echo '';
  60. }
  61. echo '';
  62. }
  63. }
  64. }
  65. closedir($dir_handle);
  66. }
  67. }
  68.  
  69. ListaKatalogow($_GET['path']);
  70. ?>
CuteOne
Pomyśl co oznacza ten błąd....

wstaw zamiast while ($file = readdir($dir_handle)) {

coś co z $file (stringa!) zrobi tablicę

  1. while ($file = readdir($dir_handle)) {
  2. $f[$file] = $file;
  3. }
  4.  
  5. ksort($f);
  6.  
  7. foreach($f as $file) {
  8.  
  9. echo $file;
  10. }
celbarowicz
nie wiem czy to zadziała, na lokalu działa.
  1. <?php
  2. $tablica_plikow2=glob("c:/*");
  3. foreach ($tablica_plikow2 as $nazwa_pliku) {$nazwa_pliku=substr($nazwa_pliku,3);
  4.  
  5. echo $nazwa_pliku;
  6. echo '<br>';}
  7.  
  8.  
  9.  
  10.  
  11.  
  12. ?>
  13.  
Kshyhoo
Cytat(CuteOne @ 21.11.2010, 23:18:04 ) *
Pomyśl co oznacza ten błąd....

No, że nie mam w $file tablicy, to już wiem winksmiley.jpg Teraz działa poprawnie:
  1. if ($dir_handle = opendir($base_dir."/".$path)) {
  2. while ($file = readdir($dir_handle)) {
  3. $f[$file] = $file;
  4. }
  5. ksort($f);
  6. foreach($f as $file) {
  7. chdir($base_dir."/".$path);
  8. if (($file != ".") && ($file != "..")) {
  9. if (is_file($file)) {

Wielkie dzięki, CuteOne.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.