Hello,

How would I be able to search a directory for all of the files that end with '.php' and count the lines that are in those files?

Thanks,
Alex Samanta

    Try this, Alex:

    $dir=opendir("/dir");
    while(false!==($file=readdir($dir))) {
    $currFile=explode(".",$file);
    if($currFile[1]=="php") {
    $readFile=fopen($file,"r");
    while(!feof($readFile)) {
    $currLine[]=fgets($readFile,255);
    }
    fclose($readFile);
    }
    }
    print(count($currLine));

    Hope that helps!

    Happy coding! 🙂

      $dir="path";
      if (is_dir($dir)){
      $handle=opendir($dir);
      $i=0;
      while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && stristr($file, ".php") {
      $files[$i]=$dir . "/" . $file;
      $i++;
      }
      }
      closedir($handle);
      if (isset($files)) {
      for ($i=0; $i<count($files); $i++) {
      if (is_file($files[$i])){
      $f = @fopen ($file, "r");
      $j=0;
      while (!feof ($f)) {
      $j++;

      }
      $file.$i = $j;
      fclose ($f);
      }
      }
      }

      what youll get is a variable for each file (file0, file1, file2) and it will contain the number of lines

        Write a Reply...