title: PHP Read file content based on pathname
body: <p><strong>UPDATED:</strong></p>
  
  <p>I am using <code>RecursiveIteratorIterator</code> to scan the directories to read <code>index.html</code> file.</p>
  
  <p>Now, based on the Pubname and Pubversion, i am trying to read the corresponding index.html file, to get the values of <code>Readability Grade</code>, <code>Readability Score</code> etc. (by storing the values in local storage)</p>
  
  <p>The values are fetched from local storage if i use the html and JS code separately, but after i integrate into php, it is not working.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>&lt;?php
  //error_reporting(0);
  
  //Get Pub version
  function get_version($path){
      $needle = "=";
      if(($pos = strpos($path, $needle)) != 0){
           $bits = str_split($path, $pos + strlen($needle));
  
           $integer_count = 0;
           for($x = 0; $x &lt; strlen($bits[1]); $x++){
               if(is_numeric($bits[1][$x])){
                   $integer_count++;
               }else{
                   break;
               }
           }
           if($integer_count &gt; 0){
               $bits = str_split($bits[1], $integer_count);
              return  $bits[0];
           }
      }
      return -1;
  }
  
  
  $it = new RecursiveDirectoryIterator("C:\Users\Sachin_S2\Desktop\Script");
  
  foreach(new RecursiveIteratorIterator($it,RecursiveIteratorIterator::SELF_FIRST) as $file) {
  
      //IGNORE FILE TYPES
      //$filetypes = array("jpg", "png", "pdf", "css", "csv","log","txt");
      $htmlfiles = array("html");
      $filetype = pathinfo($file, PATHINFO_EXTENSION);
      if (in_array(strtolower($filetype), $htmlfiles)) {
  
          // skip dot files while iterating
          $it-&gt;setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
  
          $files = array($file);
          $pathname = $file-&gt;getPathname();
          print_r($pathname);
  
          $version = get_version($pathname);
          if($version &gt; 0){
              // use $version to read correct file
              include 'html/home.html';
          }
          echo '*********************************************************************************'.'&lt;br/&gt;&lt;br/&gt;';
  
      }       
  }
  </code></pre>
  
  <p><strong>html/home.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
    &lt;meta name="description" content=""&gt;
    &lt;meta name="author" content=""&gt;
    &lt;title&gt;SEO Metrics&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
          &lt;!--Begin script code--&gt;
          &lt;form name="myform3"&gt;
              &lt;!-- 
              &lt;input type="hidden" name="formvar" value=""&gt;
              &lt;input type="file" name="file" id="file"&gt;
              &lt;p id="demo"&gt;&lt;/p&gt;&lt;br/&gt;&lt;br/&gt;
              --&gt;
  
              &lt;div class="form-group"&gt;
                &lt;label&gt;Readability Grade:&lt;/label&gt;
                &lt;input type="text" class="form-control" id="grade"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;label&gt;Readability Score:&lt;/label&gt;
                &lt;input type="text" class="form-control" id="score"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;label&gt;Total Word Count:&lt;/label&gt;
                &lt;input type="text" class="form-control" id="words"&gt;
              &lt;/div&gt;
          &lt;/form&gt;
          &lt;!--END script code--&gt;
  
        &lt;!--Custom JS code--&gt;
        &lt;script src="js/home.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>js/home.js</strong></p>
  
  <pre><code>$(document).ready(function() {
  
  
  document.getElementById('file').onchange = function() {
  
      const file = this.files[0];
      const reader = new FileReader();
  
      reader.onload = (event) =&gt; {
        const file = event.target.result;
        const allLines = file.split(/\r\n|\n/);
        let arr = allLines;
  
  
        arr.forEach((kv) =&gt; {
          if (kv.includes("Flesh-Kincaid Grade Level")) { 
                  var fetch_grade = kv.replace(/&lt;\/?[^&gt;]+(&gt;|$)/g, "");
                  var formated_grade = fetch_grade.split(":").pop();          //Remove part of the string before ":"
                  localStorage.setItem("Readability_Grade", formated_grade);
  
                  document.getElementById('grade').value = localStorage.getItem("Readability_Grade").replace(/\s/g, "");  //Assign localStorage to text box
                  localStorage["Readability_Grade"] = grade.value;
                  //alert(formatedgrade);
          }
          if (kv.includes("Flesh Reading Ease Score")) { 
                  var fetch_score = kv.replace(/&lt;\/?[^&gt;]+(&gt;|$)/g, ""); 
                  var formated_score = fetch_score.split(":").pop();
                  localStorage.setItem('Readability_Score', formated_score);
                  document.getElementById('score').value = localStorage.getItem("Readability_Score").replace(/\s/g, "");
                  //alert(formatedscore);
          }
          if (kv.includes("Reuse Metrics Score")) { 
                  var metricscore = kv; 
                  //alert(metricscore);
          }
          if (kv.includes("Total words")) { 
                  var totalwords = kv.replace(/&lt;\/?[^&gt;]+(&gt;|$)/g, "");
                  var total_words_formated = totalwords.split(":").pop();
                  localStorage.setItem('Word_Count', total_words_formated);
                  document.getElementById('words').value = localStorage.getItem("Word_Count").replace(/\s/g, ""); 
                  //alert(totalwords);
          } 
       }); 
    };
  
      reader.onerror = (event) =&gt; {
        alert(event.target.error.name);
      };
  
      reader.readAsText(file);
    };
  
  });
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <p>On reading this php script, i should be able to read index html (for each file paths - based on pubname and version), and get the values of <code>Readability_Grade</code> and score etc., which i am not able to get.</p>
  
  <p><a href="https://i.stack.imgur.com/ehqNv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ehqNv.png" alt="enter image description here"></a></p>