note 97410 added to function.readdir

From: Date: Sun, 18 Apr 2010 20:41:47 +0000
Subject: note 97410 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-168376@lists.php.net to get a copy of this message
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> ---- Server IP: 69.147.83.197 Probable Submitter: 99.229.105.49 ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- https://master.php.net/note/edit/97410 Del: integrated -- https://master.php.net/note/delete/97410/integrated Del: useless -- https://master.php.net/note/delete/97410/useless Del: bad code -- https://master.php.net/note/delete/97410/bad+code Del: spam -- https://master.php.net/note/delete/97410/spam Del: non-english -- https://master.php.net/note/delete/97410/non-english Del: in docs -- https://master.php.net/note/delete/97410/in+docs Del: other reasons-- https://master.php.net/note/delete/97410 Reject -- https://master.php.net/note/reject/97410 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168376) next »