Script Looping Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joe592
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 8:06 pm

Script Looping Problem

Post by joe592 »

I am trying to get a script to loop through all files in a directory, load each html file, loop through all img tags in the document and get check a server response header.

The script wont loop through all the files properly. It appears to loop through the img elements correctly and checks the responses.

I am at a loss why it wont loop through all the files. If I comment out the html img tag loop and out put the file loop only, it will display the whole directory.

Any ideas why the loop might not work?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Script Looping Problem

Post by requinix »

Dunno.

How about posting some code?
joe592
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 8:06 pm

Re: Script Looping Problem

Post by joe592 »

Here is the code I'm trying to use.

Code: Select all

$htmlDoc = new DomDocument();
$handle = opendir('.'); 
    while (false !== ($file = readdir($handle))) {
		$extension = strtolower(substr(strchr($file, '.'), 1));		
		if($extension == 'html'){
			echo $file . '<br>';				
			$htmlDoc->loadHTMLFile($file);		
			$element = $htmlDoc->getElementsByTagName('img');			
			foreach ($element AS $elements){			
			$array = get_headers('http://www.thermo-kinetics.com/' . $elements->getAttribute('src'));			
			if($array[0]=="HTTP/1.1 404 Not Found") {			
			echo 'http://www.thermo-kinetics.com/' . $elements->getAttribute('src') . '-->' .  '<span style="color:red">' . $array[0] . '</span><br>';			
			ob_flush();			
			flush();			
			} else if($array[0]=="HTTP/1.1 200 OK")	{			
			echo 'http://www.thermo-kinetics.com/' . $elements->getAttribute('src') . '-->' .  '<span style="color:green">' . $array[0] . '</span>br>';						
			ob_flush();			
			flush();	
			} else {			
			echo 'http://www.thermo-kinetics.com/' . $elements->getAttribute('src') . '-->' .  '<span style="color:orange">' . $array[0] . '</span><br>';			
			ob_flush();			
			flush();
			}			
			}
		}         
	}
    closedir($handle);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Script Looping Problem

Post by requinix »

Looks fine to me. What does "wont loop through all the files properly" mean?
joe592
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 8:06 pm

Re: Script Looping Problem

Post by joe592 »

What I mean by that is that the directory contains 25 html files, but the loop stops at the 9th file in the directory. When I was developing this script I wrote it in 2 parts.

1. To open the parent directory, check the extension and return only the html files.

2. To load a html file and return all img tags, then to get the response header from each img tag on the page by retrieving the src attribute of each tag and sending a request. I would then check each header for the response and if passed output "ok" per request.

When I wrote and tested each individual part it appeared to work correctly, but when I integrate the code into 1 part the file directory stops at the 9th html page. The code looks totally fine to me, but I must be doing something wrong because it wont retrieve all the html files. I am stumped.
joe592
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 8:06 pm

Re: Script Looping Problem

Post by joe592 »

After further reviewing my code, I thought that the problem must lie elsewhere. I decided to check the config file for apache and php and discovered the problem. The problem was in the php config file. There is a line in there that specifies the maximum allowable time in seconds for a script to execute. It was set by default to 30 seconds, which was not enough time for the script to execute. After changing the time to a higher value so that the script has time to fully execute, it worked perfect.

Thanks for your help. **SOLVED**
Post Reply