title: Create CSV in memory, email, and remove from memory
body: <pre><code>private function convert_to_csv($input_array, $output_file_name, $delimiter) {
  
      $temp_memory = fopen('php://memory','w');
  
      foreach ($input_array as $line) {
  
          fputcsv($temp_memory, $line, $delimiter);
  
      }
  
      fseek($temp_memory, 0);
  
      header('Content-Type: application/csv');
      header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
  
      fpassthru($temp_memory);
  
  }               
  </code></pre>
  
  <p>I use the above function to take an array of data, convert to CSV, and output to the browser. Two questions:</p>
  
  <ol>
  <li>Is the file removed from memory after being downloaded via HTTP?</li>
  <li>How would this same function be rewritten such that the file could be used (for example, to use as an email attachment sent with PHPMailer) and then removed from memory immediately afterwards?</li>
  </ol>
  
  <p><strong>EDIT: Working Code - But writes to file, not memory</strong></p>
  
  <pre><code>public function emailCSVTest() {
  
      $test_array = array(array('Stuff','Yep'),array('More Stuff','Yep yep'));
  
      $temp_file = '/tmp/temptest.csv';
  
      $this-&gt;convertToCSV($test_array, $temp_file);
  
      $this-&gt;sendUserEmail('Test Subject','Test Message','nowhere@bigfurryblackhole.com',$temp_file);
  
      unlink($temp_file);
  
  }
  
  private function convertToCSV($input_array, $output_file) {
  
      $temp_file = fopen($output_file,'w');
  
      foreach ($input_array as $line) {
  
          fputcsv($temp_file, $line, ',');
  
      }
  
      fclose($temp_file);
  
  }
  </code></pre>
  
  <p>Still unanswered: does the original function remove the file from memory, or no?</p>