Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z pętlą foreach
Forum PHP.pl > Forum > PHP
emi1
Witam, bardzo proszę was o pomoc gdy probuję ocenić zdjęcie lub dodać komentarz wywala :

  1. Warning: Invalid argument supplied for foreach() in /home/testowon/domains/strona.pl/public_html/lib/cache.php on line 53
  2.  
  3. Warning: Invalid argument supplied for foreach() in /home/testowon/domains/strona.pl/public_html/lib/cache.php on line 57
  4.  
  5. Warning: Cannot modify header information - headers already sent by (output started at /home/testowon/domains/strona.pl/public_html/lib/cache.php:53) in /home/testowon/domains/strona.pl/public_html/index.php on line 137


Kod pliku cache.php:

  1. <?php
  2.  
  3. /*
  4. The Cache class reads and writes complete pages from the cache.
  5. The name of a cache file is based on its URL
  6. */
  7.  
  8. class Cache {
  9. private $fileName = '';
  10. private $path = '';
  11. private $enabled = true;
  12. private $captured = false;
  13.  
  14. public function __construct( $path, $pageName ) {
  15. $this->path = $path;
  16. $this->fileName = $this->path . strtr( $pageName, '?&', '--' ) . '.html';
  17.  
  18. if( $_SERVER['REQUEST_METHOD'] != 'GET' ) {
  19. $this->enabled = false;
  20. }
  21. }
  22.  
  23. public function disable() {
  24. $this->enabled = false;
  25. }
  26.  
  27. public function forceEnable() {
  28. $this->enabled = true;
  29. }
  30.  
  31. public function lookup() {
  32. if( $this->enabled && file_exists($this->fileName) ) {
  33. $fp = fopen($this->fileName, 'rb');
  34. fpassthru($fp);
  35. fclose($fp);
  36. exit();
  37. }
  38. }
  39.  
  40. public function capture() {
  41. if( $this->enabled ) {
  42. $this->captured = true;
  43. }
  44. }
  45.  
  46. public function clear( $pattern = '' ) {
  47. require_once( 'lib/filesystem.php' );
  48. if( empty($pattern) && !empty($this->path) ) {
  49. Filesystem::rmdirr( $this->path, true );
  50. } else {
  51. $files = glob( $this->path . 'all/view/' . strtr( $pattern, '?&', '--' ) . '.html' );
  52. foreach( $files as $f ) {
  53. unlink( $f );
  54. }
  55. $files = glob( $this->path . 'user/*/view/' . strtr( $pattern, '?&', '--' ) . '.html' );
  56. foreach( $files as $f ) {
  57. unlink( $f );
  58. }
  59. }
  60. }
  61.  
  62. public function write() {
  63. if( $this->captured ) {
  64. require_once( 'lib/filesystem.php' );
  65. Filesystem::mkdirr( dirname($this->fileName) );
  66. file_put_contents( $this->fileName, ob_get_contents() );
  67. }
  68. }
  69. }
  70.  
  71. ?>



Ktos wie jak to naprawić?

Pozdrawiam
Uriziel01
Pomiędzy linią 52 a 53 zrób var dump'a z zmiennej $files,z pewnościa nie ma tam tablicy.
Sephirus
Wykorzystana funkcja glob() zwraca tablice plików, pustą tablicę lub FALSE w przypadku błędu... Normalnym założeniem jest, że żaden rodzaj tablicy nie popsuje pętli foreach więc na bank funkcja ta zwraca false czyli pokićkałeś coś w tych adresach/ścieżkach albo jest coś nie tak z przekazywana do metody zmienną $pattern

EDIT: Poza tym

Cytat
On some systems it is impossible to distinguish between empty match and an error.


Najlepszym rozwiązaniem będzie sprawdzenie dokładne tych ścieżek - a także sprawdź czy skrypt przeszukujący te ścieżki - ma do nich uprawnienia odczytu - bo ta funkcja też może wywalać false w momencie gdy nie ma uprawnień odczytu...
emi1
Dzięki chłopaki za zainteresowanie wink.gif

Po dodaniu vad dump'a pokazało te same błędy tyle że w pierwszej lini "bool(false)".

Po tym jak owe błędy pokażą się i wróce na strone następnie oswieże to komentarz który dodalem wczesniej jest normalnie wyswietlony razem z ocena.





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.