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?
Script Looping Problem
Moderator: General Moderators
Re: Script Looping Problem
Dunno.
How about posting some code?
How about posting some code?
Re: Script Looping Problem
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);
Re: Script Looping Problem
Looks fine to me. What does "wont loop through all the files properly" mean?
Re: Script Looping Problem
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.
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.
Re: Script Looping Problem
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**
Thanks for your help. **SOLVED**