title: Exporting a large CSV via PHP results in timeout/memory allocate error
body: <p>I have a tool that allows data to be exported based on different time ranges. The problem I run into is that the longer the time range, the more data, and eventually the data set is too large -- resulting in either a timeout error or a memory allocation error.</p>
  
  <p>Short of changing php.ini to have a larger max_execution_time, etc, is there a better way of doing this?</p>
  
  <p>Here's my script to build the CSV (the user selects export, then this page is loaded):</p>
  
  <pre><code>$fileName           = "export-".date("YmdHis");
  $exportSignature    = array(" ","Export","Generated by: ".$_SESSION['name'],date("Y-m-d H:i:s"));
  
  header('Content-Type: text/csv; charset=utf-8');
  header('Content-Disposition: attachment; filename='.$fileName.'.csv');
  
  // create a file pointer connected to the output stream
  $output = fopen('php://output', 'w');
  
  // run all the queries for the data
  if($exportType == "list") {
      include('include/query-compare.php');   
  } elseif($exportType == "analyze") {
      include('include/query-analyze.php');   
  } elseif($exportType == "funnel") {
      include('include/query-funnel.php');    
  } elseif($exportType == "funnelTrend") {
      include('include/query-funnel-trends.php'); 
  }
  
  // add column headers
  fputcsv($output,$columnHeaders);
  
  // add row data
  if($exportType == "list") {
      foreach($comparison as $account) {
          fputcsv($output,$account);
      }
  } elseif($exportType == "analyze") {
      foreach($analyze as $account) {
          fputcsv($output,$account);  
      }
  } elseif($exportType == "funnel" || $exportType == "funnelTrend") {
      foreach($funnel as $line) {
          fputcsv($output,$line); 
      }
  }   
  
  // add export signature
  foreach($exportSignature as $line) {
      fputcsv($output,array($line));
  }
  </code></pre>
  
  <p>Here's a sample of the array that's used to create the rows. This is the variable -- if the time range is small there might be a few hundred entries in the array, if it's large, there's tens of thousands.</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; 1
              [firstName] =&gt; Marco
              [lastName] =&gt; R.
              [title] =&gt; D
              [email] =&gt; test@test.com
              [company] =&gt; xx
              [lSource] =&gt; xx
              [lDetail] =&gt; xx
              [lExact] =&gt; xx
              [createdAt] =&gt; 2017-06-26 00:00:00
              [nDate] =&gt; 2017-08-15
              [cDate] =&gt; 2017-08-15
              [cMoment] =&gt; xx
              [mDate] =&gt; 2017-08-15
              [mMoment] =&gt; xx
              [Id] =&gt; 003Axx
              [Type] =&gt; Contact
              [accountId] =&gt; 001A0xx
              [parentAccountId] =&gt; 
              [accountOwner] =&gt; Kevin S.
              [xx] =&gt; Nicholas W.
              [accountType] =&gt; Prospect
              [totalARR] =&gt; 0.00
              [accountTier] =&gt; 
              [ty] =&gt; XX
              [industry] =&gt; Corporate Services
              [secondaryIndustry] =&gt; IT Services and Consulting
              [billingCountry] =&gt; Germany
              [1] =&gt; 006Axx
              [2] =&gt; PS (New Business)
              [3] =&gt; Kevin S.
              [4] =&gt; Nicholas W.
              [5] =&gt; cc
              [6] =&gt; New Business
              [7] =&gt; Identify
              [8] =&gt; 40000.00
              [9] =&gt; 2017-08-16
              [10] =&gt; 2018-07-27
              [11] =&gt; 2017-08-16
              [12] =&gt; 2017-08-21
          )
  )
  </code></pre>
  
  <p>I'd be fine with scheduling the export and emailing it to the user as well, I'm just not familiar with how I can have this large CSV constructed in the background without timing out.</p>
  
  <p><strong>EDIT:</strong> would it be a better option to queue the export in a database and have a cron job that looks for the exports, then runs the export in the background until complete? Since it's running via the cron it shouldn't have a timeout, right?</p>