title: A PHP Error was encountered Severity: Warning Message: fwrite(): 39 is not a valid stream resource
body: <p>Below is the code where I attempt to write results from database into a text file. But I keep getting the error message as above. I suppose this has something do with syntax yet I am not able to resolve it. Please share yours on how to figure it out.  </p>
  
  <pre><code>     public function createfile(){
  
          $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('crd_article_desc');
          $query = $this-&gt;db-&gt;get();
          $result=$query-&gt;result_array();         
  
      $txt ='';
      $counter = 1;                       
      foreach($result as $results){
  
      $vp_string = trim($results['article_title']);
  
      $vp_string = html_entity_decode($vp_string);
  
      $vp_string = strip_tags($vp_string);
  
      $vp_string = strtolower($vp_string);
  
      $vp_string = preg_replace('~[^ a-z0-9_.]~', ' ', $vp_string);
  
      $vp_string = preg_replace('~ ~', '-', $vp_string);
  
      $vp_string = preg_replace('~-+~', '-', $vp_string);
  
      $new_friendly_url = $vp_string; 
      echo $results['article_title'];
      $looptxt = $results['article_id']."-".$new_friendly_url."\n";
  
      $txt=$txt.$looptxt;
  
      fwrite($myfile,$txt);
      fclose($myfile);
  }
  </code></pre>