Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]problem z galeria
Forum PHP.pl > Forum > Przedszkole
beba
Kod:

  1. <?php
  2. function createGallery( $pathToImages, $pathToThumbs )
  3. {
  4. echo "Creating gallery.html <br />";
  5.  
  6. $output = "<html>";
  7. $output .= "<head><title>Thumbnails</title></head>";
  8. $output .= "<body>";
  9. $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
  10. $output .= "<tr>";
  11.  
  12. // open the directory
  13. $dir = opendir( $pathToThumbs );
  14.  
  15. $counter = 0;
  16. // loop through the directory
  17. while (false !== ($fname = readdir($dir)))
  18. {
  19. // strip the . and .. entries out
  20. if ($fname != '.' && $fname != '..')
  21. {
  22. $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
  23. $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
  24. $output .= "</a></td>";
  25.  
  26. $counter += 1;
  27. if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
  28. }
  29. }
  30. // close the directory
  31. closedir( $dir );
  32.  
  33. $output .= "</tr>";
  34. $output .= "</table>";
  35. $output .= "</body>";
  36. $output .= "</html>";
  37.  
  38. // open the file
  39. $fhandle = fopen( "gallery.html", "w" );
  40. // write the contents of the $output variable to the file
  41. fwrite( $fhandle, $output );
  42. // close the file
  43. fclose( $fhandle );
  44. }
  45. // call createGallery function and pass to it as parameters the path
  46. // to the directory that contains images and the path to the directory
  47. // in which thumbnails will be placed. We are assuming that
  48. // the path will be a relative path working
  49. // both in the filesystem, and through the web for links
  50. createGallery("../rsah/upload/","../rsah/upload/thumbs/");
  51. ?>
Wyskakuje mi taki błąd przy tworzeniu miniatury zdjęcia

Fatal error: Allowed memory size of 73400320 bytes exhausted (tried to allocate 73138147 bytes) in

Czy to wina kodu, czy małej ilości memory size ustawionej w php.ini?
SmokAnalog
Nie lepiej wyświetlać galerię dynamicznie zamiast zapisywać ją do pliku .html?

Jeśli jednak bardzo chcesz lub z jakichś względów musisz tak zrobić, to zamiast zbierać cały $output do jednej zmiennej, z każdą iteracją pętli wywołuj po prostu fwrite. Wtedy będziesz zapisywał do pliku po kawałku i nie zabraknie pamięci.
beba
dynamicznie tzn jak? od razu w pętli odczytywać z katalogu,bez tworzenia galeria.html ?
SmokAnalog
Dokładnie tak. Jeśli Twoja strona nie ma jakiejś ogromnej liczby odwiedzin, to nie wpłynie to znacząco na czas ładowania strony.

Coś w ten deseń (pisane z palca, bez testowania, więc z góry sorki za błędy):
  1. <html>
  2. <head>
  3. <title>Thumbnails</title>
  4. </head>
  5. <body>
  6. <table cellspacing="0" cellpadding="2" width="500">
  7. <tr>
  8. <?php
  9. // open the directory
  10. $pathToThumbs = '../rsah/upload/thumbs/';
  11. $dir = opendir($pathToThumbs);
  12.  
  13. $counter = 0;
  14. // loop through the directory
  15. while (false !== ($fname = readdir($dir))) {
  16. // strip the . and .. entries out
  17. if ($fname != '.' && $fname != '..') { ?>
  18. <td valign="middle" align="center">
  19. <a href="../rsah/upload/<?php echo $fname ?>">
  20. <img src="<?php echo $pathToThumbs ?><?php echo $fname ?>" border="0" />
  21. </a>
  22. </td>
  23. <?php
  24. $counter += 1;
  25. if ( $counter % 4 == 0 ) : ?>
  26. </tr><tr>
  27. <?php endif ?>
  28. <?php
  29. }
  30. }
  31. // close the directory
  32. closedir( $dir );
  33. ?>
  34. </tr>
  35. </table>
  36. </body>
  37. </html>
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.