note 97410 deleted from function.readdir by degeberg

From: Date: Sun, 18 Apr 2010 21:21:48 +0000
Subject: note 97410 deleted from function.readdir by degeberg
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168380@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- Here is a script which opens the parent directory, reads all contents and returns only files with an html extension. Then it loop through all html anchor elements, retrieves the href attribute. It then sends a http request on each and every anchor element and returns a server response. Which is checked for validity and returns a color coded response. <html> <head> <style> .passed { position:absolute; top:1em; } .failed { position:absolute; top:2em; } .mailto { position:absolute; top:3em; } .other { position:absolute; top:4em; } .finishedScript { position:absolute; top:5em; } .results { position:absolute; top:6em; } .resultCount { left:7em; } </style> </head> <body> <b class="passed"> Links Passed: </b> <b class="failed"> Links Failed: </b> <b class="mailto"> Mailto: </b> <b class="other"> Other: </b> <?php $startTime = time(); $htmlDoc = new DomDocument(); $passed = 0; $failed = 0; $other = 0; $mailto = 0; $strThermo = 'http://www.thermo-kinetics.com/'; $strRed = '<span style="color:red">'; $strGreen = '<span style="color:green">'; $strOrange = '<span style="color:orange">'; $strNotFound = 'HTTP/1.1 404 Not Found'; $strFound = 'HTTP/1.1 200 OK'; ob_start(); echo '<span class="results">'; $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('a'); foreach ($element AS $elements){ $strLength = strlen($elements->getAttribute('href')); //$strSubString = substr(); if (substr($elements->getAttribute('href'),0,($strLength - (($strLength * 2) - 4))) == 'http'){ $array = get_headers($elements->getAttribute('href')); if($array[0]==$strNotFound) { $failed++; echo $elements->getAttribute('href') . '-->' . $strRed . $array[0] . '</span><br>'; ob_flush(); flush(); } else if($array[0]==$strFound) { $passed++; echo $elements->getAttribute('href') . '-->' . $strGreen . $array[0] . '</span><br>'; ob_flush(); flush(); } else { $other++; echo $elements->getAttribute('href') . '-->' . $strOrange . $array[0] . '</span><br>'; ob_flush(); flush(); } } else if (substr($elements->getAttribute('href'),0,($strLength - (($strLength * 2) - 6))) == 'mailto'){ $mailto++; echo $elements->getAttribute('href') . '-->' . $strGreen . 'Mailto ignored </span><br>'; } else { $array = get_headers($strThermo . $elements->getAttribute('href')); if($array[0]==$strNotFound) { $failed++; echo $strThermo . $elements->getAttribute('href') . '-->' . $strRed . $array[0] . '</span><br>'; ob_flush(); flush(); } else if($array[0]==$strFound) { $passed++; echo $strThermo . $elements->getAttribute('href') . '-->' . $strGreen . $array[0] . '</span><br>'; ob_flush(); flush(); } else { $other++; echo $strThermo . $elements->getAttribute('href') . '-->' . $strOrange . $array[0] . '</span><br>'; ob_flush(); flush(); } } } } } closedir($handle); echo '</span>'; $finishTime = time(); $totalTime = (($finishTime - $startTime) / 60); echo '<span class="finishedScript">' . ($passed+$failed+$other) . ' links processed in ' . $totalTime . ' minutes.</span><br>'; echo '<span class="passed resultCount" style="color:green">' . $passed . '</span><br>'; echo '<span class="failed resultCount" style="color:red">' . $failed . '</span><br>'; echo '<span class="mailto resultCount" style="color:green">' . $mailto . '</span><br>'; echo '<span class="other resultCount" style="color:orange">' . $other . '</span><br>'; ?> </body> </html>

« previous php.notes (#168380) next »