
I got some very big files today and run them with my program and I got "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)".
My colleague told me to update the program so that it can split the file into smaller files (which I am not sure how to achieve this with PHP?)? Or are there any other ways to solve this problem?
Below is my code -
Code: Select all
<?php
if (($handle = fopen($_POST["file1"], "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn1 = 0;
while (($data = fgetcsv($handle, 0, "¦")) !== FALSE) {
# Count the total keys in the row.
$c1 = count($data);
# Populate the multidimensional array.
for ($x=0;$x<$c1;$x++)
{
$csvarray1[$nn1][$x] = $data[$x];
//echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
//echo "<br />\n";
}
$nn1++;
//echo "<br />\n";
}
# Close the File.
fclose($handle);
}
if (($handle = fopen($_POST["file2"], "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn2 = 0;
while (($data = fgetcsv($handle, 0, "¦")) !== FALSE) {
# Count the total keys in the row.
$c2 = count($data);
# Populate the multidimensional array.
for ($x=0;$x<$c2;$x++)
{
$csvarray2[$nn2][$x] = $data[$x];
//echo "$csvarray2($nn2)($x) = ". $csvarray2[$nn2][$x] . "<br />\n";
//echo "<br />\n";
}
$nn2++;
//echo "<br />\n";
}
# Close the File.
fclose($handle);
}
function array_diff_no_cast(&$ar1, &$ar2) {
$diff = Array();
foreach ($ar1 as $key => $val1) {
if (array_search($val1, $ar2) === false) {
$diff[$key] = $val1;
}
}
return $diff;
}
$result1 = array_diff_no_cast($csvarray1,$csvarray2);
$result2 = array_diff_no_cast($csvarray2,$csvarray1);
//output the 2 results array in a table