verzeichnis auslesen und dateien durchsuchen

Rev

Erfahrenes Mitglied
hi
ich hab folgendes prob:
ich hab mir nen script geschrieben um chatlogs zu durchsuchen
PHP:
<?php
$a="test";
$b="test2";
$directory = "log/"; 
$handle = opendir($directory); 
while ($file = readdir ($handle)) 
{
 $fd = fopen($file,"r");
 $seite = fread ($fd, filesize ($file));
 $seite=strtolower($seite);
 fclose ($fd);

if (strstr($seite,$a)) 
 {
  if(strstr($seite,$b))
  {
  print($file);
  }
 }
}
?>

das ganze gibt dann folgende fehler:

Warning: fopen(".", "r") - Permission denied in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("..", "r") - Permission denied in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%21.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %21.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%22followme%22_meine_s%fc%dfe_obachtgeberin_g.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %22followme%22_meine_s%fc%dfe_obachtgeberin_g.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%23.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %23.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%26nbsp%3b.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %26nbsp%3b.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%26shy.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %26shy.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%3a%28.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %3a%28.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%3ao%29.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %3ao%29.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%3f%3f%3f%3f.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %3f%3f%3f%3f.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

Warning: fopen("%40home.html", "r") - No such file or directory in c:\programme\apache group\apache\htdocs\isa\index.php on line 9

Warning: stat failed for %40home.html (errno=2 - No such file or directory) in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fread(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 10

Warning: fclose(): supplied argument is not a valid File-Handle resource in c:\programme\apache group\apache\htdocs\isa\index.php on line 12

das ganze läuft lokal auf meiner 2000er maschine..
weiß jemand wo der fehler ist?
 
Also... erstens musst du vor dem Öffnen erst mal das Verzeichnis mit berücksichtigen, denn sonst nimmt PHP an, dass du eine Datei öffnen willst, die im gleichen Verzeichnis wie das Script liegt. Also musst du das Verzeichnis vor den Dateinamen hängen.

Dann zweitens, vor dem Öffnen musst du außerdem überprüfen, ob es sich überhaupt um eine Datei handelt. Könnte ja auch ein Verzeichnis oder ein Link sein. Also mit is_file() auf Datei testen, wenn's keine ist, weiter mit dem nächsten Schleifendurchlauf.

Drittens kannst du beide if-Anweisungen zu einer zusammenziehen. Ist zwar kein Fehler, das anders zu schreiben, sieht aber schöner aus.

Hier die korrigierte Version:
PHP:
<?php
$a = "test";
$b = "test2";
$directory = "./log/"; 
$handle = opendir($directory); 

while ($file = readdir ($handle)) {
	$file = $directory.$file;
	if (!is_file($file)) continue;
	$fd = fopen($file,"r");
	$seite = fread ($fd, filesize ($file));
	$seite = strtolower($seite);
	fclose ($fd);

	if (strstr($seite,$a) && strstr($seite,$b)) {
		print($file);
	}
}
?>


reima
 
hi
vielen dank erstmal..
nur eine frage

kann es sein, dass er dateinamen mit sonderzeichen nicht beachtet?
also zb
"log%a0zusammenschneiden.html"
 
Zurück