title: Problems comparing two large files in PHP
body: <br>
  I would like to compare two files and create a new one with the result of both. Basically the user uploads the .csv file and then the program reads the file and compares it with a .txt file. Then it creates a new file with the results of the files.
  For example:
  User .csv file:
  <p>RSID,CHROMOSOME,POSITION,RESULT<br>
  rs9651229,1,567667,CC<br>
  rs9701872,1,568208,TT<br>
  rs11497407,1,568527,GG<br>
  rs116587930,1,727841,GG<br>
  rs3131972,1,752721,GG<br>
  rs114525117,1,759036,GG<br>
  rs12127425,1,794332,GG<br>
  rs79373928,1,801536,TT<br>
  rs116452738,1,834830,GG<br>
  rs72631887,1,835092,TT<br>
  rs4970383,1,838555,CC<br>
  rs28678693,1,838665,TT<br>
  rs4970382,1,840753,CC<br></p>
  <p>Template .txt file:</p>
  <h1>Comments</h1>
  <h1>rsid  chromosome  position    genotype</h1>
  <p>rs548049170 1   69869<br />
  rs13328684  1   74792<br />
  rs9283150   1   565508<br />
  i713426 1   726912<br />
  rs116587930 1   727841<br />
  rs3131972   1   752721<br />
  rs12184325  1   754105<br />
  rs12567639  1   756268<br />
  rs114525117 1   759036<br />
  rs12124819  1   776546<br />
  rs12127425  1   794332<br />
  rs79373928  1   801536<br />
  rs72888853  1   815421<br />
  rs7538305   1   824398<br />
  rs28444699  1   830181</p>
  <p>Result .txt file:</p>
  <p>rs548049170 1   69869   --<br>
  rs13328684  1   74792   --<br>
  rs9283150   1   565508  --<br>
  i713426 1   726912  --<br>
  rs116587930 1   727841  GG<br>
  rs3131972   1   752721  AG<br>
  rs12184325  1   754105  --<br>
  rs12567639  1   756268  --<br>
  rs114525117 1   759036  GG<br>
  rs12124819  1   776546  --<br>
  rs12127425  1   794332  GG<br>
  rs79373928  1   801536  TT<br>
  rs72888853  1   815421  --<br>
  rs7538305   1   824398  --<br>
  rs28444699  1   830181  --<br></p>
  <p>In short, where there is no result, put --, otherwise, put the value.
  I created this code:</p>
  <p>index.php:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;pt-br&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Convertendo dados brutos para outras plataformas&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div align=&quot;center&quot;&gt;
      &lt;form method = &quot;POST&quot; action = './upload.php'  enctype='multipart/form-data'&gt;
          &lt;input type = &quot;file&quot; id = &quot;arquivo&quot; name = &quot;arquivo&quot;&gt;&lt;/input&gt;
          &lt;/br&gt;
          &lt;button type=&quot;submit&quot;&gt;Salvar&lt;/button&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>upload.php</p>
  <pre><code>&lt;?php
  //Create the array
  $dna= array();
  // check there are no errors
  if($_FILES['arquivo']['error'] == 0){   //Receive the file from the user
      $name = $_FILES['arquivo']['name'];
      $ext = strtolower(end(explode('.', $name)));
      $name2 = strtolower((explode('.', $name)[0]));
      $type = $_FILES['arquivo']['type'];
      $tmpName = $_FILES['arquivo']['tmp_name'];
  
      // check the file is a csv
      if($ext === 'csv'){
          if(($handle = fopen($tmpName, 'r')) !== FALSE) {
              // necessary if a large csv file
              $data = fgetcsv($handle, 1000, ',');
              $row = 0;
  
              while(($data = fgetcsv($handle, 100, ',')) !== FALSE) {
                  array_push($dna, [&quot;RSID&quot; =&gt; $data[0], &quot;CHROMOSOME&quot; =&gt; $data[1], &quot;POSITION&quot; =&gt; $data[2], &quot;RESULT&quot; =&gt; $data[3]]);
                  $row++;
              }
              fclose($handle); //Close the csv file
          }
          
          $txt_file = fopen('Template_23andme_v5.txt','r'); //Template that I would like to compare
          $modelo = array(); //Create a array
          $a = 1;
          while ($a &lt;= 20) { //Taking out the comments
              $line = fgets($txt_file);
              $a++;
          }
          while ($line = fgets($txt_file)) {
              $line2 = explode(&quot;\t&quot;, $line); //Separating the spaces
              array_push($modelo, [&quot;RSID&quot; =&gt; $line2[0], &quot;CHROMOSOME&quot; =&gt; $line2[1], &quot;POSITION&quot; =&gt; $line2[2], &quot;RESULT&quot; =&gt; &quot;--&quot;]); //Putting the array
          }
          fclose($txt_file); //Close the txt file
          //Creating a new file
          header(&quot;Content-disposition: attachment; filename=$name2.txt&quot;);
          header(&quot;Content-type: text/plain&quot;);
          $output = fopen( 'php://output', 'w' );
          $pos = 0;
          foreach ($modelo as $linha) {
              $key = array_search($linha[&quot;RSID&quot;], array_column($dna, &quot;RSID&quot;));
              if(!empty($key)){ //If find the RSID, put the result in the array
                  $modelo[$pos]['RESULT'] = $dna[$key]['RESULT'];
              }
              echo $modelo[$pos][&quot;RSID&quot;].&quot;\t&quot;.$modelo[$pos][&quot;CHROMOSOME&quot;].&quot;\t&quot;.$modelo[$pos][&quot;POSITION&quot;].&quot;\t&quot;.$modelo[$pos][&quot;RESULT&quot;].&quot;\n&quot;; //Putting the array with RESULT
              $pos++;
          }
          fclose($output);
      }
      
  }
  </code></pre>
  <p>If the user puts a small file, the program works perfectly, however, if the user puts a 16 mb file, for example, the program gives an error. Can someone help me?</p>
  <p>Files that I uses it. <a href="https://mega.nz/file/LM0kHYZY#0k7ZmVAISYVEyeuQeVOPu8gzKDIUkN6_rryCoM8wJxs" rel="nofollow noreferrer">link</a></p>