What tools or programs can be used to split large CSV files for easier processing in PHP?
When dealing with large CSV files in PHP, it can be beneficial to split the file into smaller chunks for easier processing. This can help prevent memory issues and improve performance. One way to achieve this is by using tools or programs that can split the CSV file into smaller parts based on a specified number of rows or file size.
// Split large CSV file into smaller chunks
$csvFile = 'large_file.csv';
$rowLimit = 1000; // Number of rows per chunk
$handle = fopen($csvFile, 'r');
$row = 0;
$chunk = 1;
if ($handle !== false) {
while (($data = fgetcsv($handle)) !== false) {
if ($row % $rowLimit == 0) {
if ($row > 0) {
fclose($output);
}
$output = fopen('chunk_' . $chunk . '.csv', 'w');
$chunk++;
}
fputcsv($output, $data);
$row++;
}
fclose($output);
fclose($handle);
}
Related questions
-
file downloads PHP client safety
-
array_merge array_combine array_intersect
-
GD library imagecreatefromjpeg imagecopyresampled