Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

PHP Read in Title from a file using fgets

Example: PHP - Read in Title from a File

<?
// open directory
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
   if ($entryName != "." && $entryName != "..") {
      $dirArray[] = $entryName;
   }
}
// close directory
closedir($myDirectory);

//   count elements in array
$indexCount   = count($dirArray);
sort($dirArray);
print("<table border=1 cellpadding=5 cellspacing=0 class=whitelinks style=\"margin-right: 130px;\">\n");
print("<tr><th>filename</th><th>real name</th><th>title</th></tr>\n");

for($index=0; $index < $indexCount; $index++){
   $realname = basename("$dirArray[$index]", ".php");
   $realname = eregi_replace("_", " ", $realname);
   $realname = ucwords($realname);

   //   read inthe file a chunk at a time until we find title, (case insensative), lets assume it's all on it's own line.
   //   is it a file?
   if(is_file("$dirArray[$index]")){
      $file=fopen("$dirArray[$index]",r);
      while (!feof($file)){
         $read = fgets($file,255);
         if (stristr($read,"<title>")){
            $title=strip_tags($read);
         }
      }
      fclose($file);
   }else{
      $title="";
   }

   print("<tr>");
   print("<td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>\n");
   print("<td>$realname</td>\n");
   print("<td>$title</td>\n");
   print("</tr>\n");
}
print("</table>\n");

?>

Share this!