Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Zero length (empty) CSV but only for larger datasets
    primarykey
    data
    text
    <p>Although my application is built using the Yii framework this is more of a general PHP issue (I think).</p> <p>I have some code which takes a Yii <a href="http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider" rel="nofollow">CActiveDataProvider</a>, loops over it and builds a CSV export. This works great and correctly builds and sends the CSV.</p> <p>I encounter a problem when trying to export a larger dataset. I have successfully output ~2500 records without any problem but when I run the exact same code for a larger set of data (~5000 records) the script appears to run ok but sends a zero length/blank CSV. I can't figure out why... it seems to run for a while and then sends the CSV, no errors or warnings in the logs. Could it be that the output is being flushed or similar before it's ready?</p> <p>Code is as follows (added a couple of inline comments here for clarity):</p> <pre><code>&lt;?php header('Content-type: text/csv'); header('Content-Disposition: attachment; filename="vacancies.csv"'); set_time_limit(240); // I know this is arbitrarily long, it's just to avoid any timeout $outstream = fopen("php://output", 'w'); $headings = array( $vacancy-&gt;getAttributeLabel('vacancy_id'), // this is a Yii method that returns the active record attribute as a string ... ); fputcsv($outstream, $headings, ',', '"'); foreach($dp-&gt;getData() as $vacancy){ // the getData() method pulls the next active record model out of the Yii dataprovider and the values for various attributes are set below $row = array( $vacancy-&gt;vacancy_id, ... ); fputcsv($outstream, $row, ',', '"'); } fclose($outstream); ?&gt; </code></pre> <p>Any thoughts on why this is working ok up to a certain number of records?</p> <p><strong>Update</strong> After re-checking the logs as suggested below I've found I am in fact running out of memory, doh!</p> <p>I can write out to the filesystem and that gets me up to about 3000 records but then runs out of memory. Any idea of the best way to alter my code to avoid running out of memory? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload