89413

fputcsv does not work with large arrays

Question:

I'm using fputcsv to (obviously) create a csv file of a table in an application. I simply post a 2d array of data to:

function formatoutput(&$vals, $key){ foreach($vals AS $row=>&$cell){ $cell = str_replace('"', '""', $cell); $cell = str_replace(array("\n",','), '', $cell); } } function outputCSV($array) { $outstream = fopen("php://output", "w"); function __outputCSV(&$vals, $key, $filehandler) { fputcsv($filehandler, $vals); } array_walk($array, "formatoutput"); array_walk($array, "__outputCSV", $outstream); fclose($outstream); } outputCSV(unserialize($_POST['data']));

so, for each line in the array, it appends it to the csv file. Well, this works fine until I pass in an array with around 500 or more lines.

Is there a size limit when using this function? The documentation doesn't mention anything. Or is there a PHP setting that would limit the size?

There is this question here: <a href="https://stackoverflow.com/questions/11249850/is-there-a-file-size-limit-when-creating-a-csv-file-using-php-and-mysql" rel="nofollow">is there a file size limit when creating a csv file using php and mysql?</a>

Which basically asks the same thing, but even though there is a marked answer, it doesn't really explain any possible solutions. It simply states:

"Sounds like a problem with your environment settings - not with the code or a limitation of the technologies."

Answer1:

PHP loads variables like these into memory. And every script has a maximum amount of memory it is allowed to use, based on your php.ini settings.

Also, when variables are copied, they can take up double the space in memory. The source memory, and the destination memory. I'm not sure the internals of fputcsv, but it's possible that the entire array is being converted to a csv "object" internally, then written out to file.

So, remember that you're possibly doubling your memory usage any time you copy.

In the very least, check your "memory_limit" in your php.ini. If you need to increase it for one script in particular, then you can set it in your script with:

ini_set('memory_limit','16M');

Recommend

  • Sum up values in multidimensional php array [closed]
  • PHP Clean Up Permutated Array
  • Magento 2 Data Migration Edit Product Unable to unserialize value
  • Fastest serialize data format form PHP reading
  • how to serialize phpseclib?
  • How to unset elements using array_walk_recursive
  • Why is fread() stopping at a string of 0s?
  • How do I write to a text file in MATLAB?
  • How do I transform events in Flume and send them to another channel?
  • reading off multiple CSV files
  • Transfer from Server > Server > Client without download to server
  • Store array in cookie
  • Python Pandas: Adding methods to class pandas.core.series.Series
  • How to get a list of all blobs in a repository in Git
  • Get the followers on a post efficient way in laravel 5.1
  • Yii: any way to save the images in compressed form?
  • Encode string to match encoded form field name in PHP POST array
  • Passing parameter through “window.location.href”
  • array_search() in Session Array
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Perl system calls when running as another user using sudo
  • Deserializing XML into class C#
  • Upload files with Ajax and Jquery
  • Large data - storage and query
  • Do I've to free mysql result after storing it?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • WOWZA + RTMP + HTML5 Playback?
  • A cron job substitute?
  • json Serialization in asp
  • python draw pie shapes with colour filled
  • How to Embed XSL into XML