Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Created July 20, 2024 22:53
Show Gist options
  • Save vfontjr/613ff5a5c3a6452d4e65b39bc1887899 to your computer and use it in GitHub Desktop.
Save vfontjr/613ff5a5c3a6452d4e65b39bc1887899 to your computer and use it in GitHub Desktop.
Split CSV Files into smaller chunks
<?php
/*
This function was created by ChatGPT and does the following:
1. Checks if the input file exists.
2. Opens the input CSV file for reading.
3. Ensures the output directory exists, creating it if necessary.
4. Reads the header row of the CSV.
5. Loops through each row of the input CSV file.
6. Creates a new output file every 400 rows, including the header row.
7. Writes each row to the current output file.
8. Closes the input and output files appropriately.
Make sure to update the $inputFile and $outputDir variables with the correct paths for your files.
*/
// Example usage:
$inputFile = 'formidable_entries.csv';
$outputDir = 'order-form-chunks';
splitCSV($inputFile, $outputDir);
function splitCSV($inputFile, $outputDir, $rowsPerFile = 400) {
// Check if the input file exists
if (!file_exists($inputFile)) {
throw new Exception("Input file does not exist.");
}
// Open the input CSV file for reading
$inputHandle = fopen($inputFile, 'r');
if (!$inputHandle) {
throw new Exception("Failed to open the input file.");
}
// Ensure the output directory exists
if (!is_dir($outputDir)) {
mkdir($outputDir, 0777, true);
}
$fileCount = 0;
$rowCount = 0;
$headers = fgetcsv($inputHandle); // Read the header row
$outputHandle = null;
while (($data = fgetcsv($inputHandle)) !== false) {
if ($rowCount % $rowsPerFile === 0) {
// Close the current output file if open
if ($outputHandle) {
fclose($outputHandle);
}
// Create a new output file
$fileCount++;
$outputFileName = $outputDir . '/output_' . $fileCount . '.csv';
$outputHandle = fopen($outputFileName, 'w');
if (!$outputHandle) {
throw new Exception("Failed to create output file: " . $outputFileName);
}
// Write the header to the new output file
fputcsv($outputHandle, $headers);
}
// Write the current row to the output file
fputcsv($outputHandle, $data);
$rowCount++;
}
// Close the last output file
if ($outputHandle) {
fclose($outputHandle);
}
// Close the input file
fclose($inputHandle);
echo "Splitting completed. Created " . $fileCount . " files.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment