Skip to main content
TopMiniSite

Back to all posts

How to Combine Multiple CSV Files In PHP?

Published on
4 min read
How to Combine Multiple CSV Files In PHP? image

Best CSV File Handling Tools to Buy in October 2025

1 Schlage F10 CSV ELA 626 Commercial Series Elan Door Lever, Hall & Closet Passage Lock, Satin Chrome

Schlage F10 CSV ELA 626 Commercial Series Elan Door Lever, Hall & Closet Passage Lock, Satin Chrome

  • IDEAL FOR INTERIOR DOORS: PERFECT FOR HALL/CLOSET DOORS, EASY ENTRY.

  • QUICK DIY INSTALLATION: HASSLE-FREE SETUP IN MINUTES WITH MINIMAL TOOLS.

  • DURABLE & CERTIFIED: PREMIUM METAL DESIGN WITH TOP SECURITY RATINGS.

BUY & SAVE
$33.92
Schlage F10 CSV ELA 626 Commercial Series Elan Door Lever, Hall & Closet Passage Lock, Satin Chrome
2 Schlage F10 CSV ORB 626 Commercial Series Orbit Door Knob, Hall & Closet Passage Lock, Satin Chrome

Schlage F10 CSV ORB 626 Commercial Series Orbit Door Knob, Hall & Closet Passage Lock, Satin Chrome

  • IDEAL FOR INTERIOR DOORS: EASY PASSAGE FUNCTION, NO LOCK NEEDED.

  • QUICK INSTALL: SELF-ALIGNING HOLES; UPGRADE IN MINUTES WITH A SCREWDRIVER.

  • DURABLE DESIGN: PREMIUM METAL, GRADE 2/AAA CERTIFIED FOR RELIABILITY.

BUY & SAVE
$22.79 $27.99
Save 19%
Schlage F10 CSV ORB 626 Commercial Series Orbit Door Knob, Hall & Closet Passage Lock, Satin Chrome
3 Software Design by Example

Software Design by Example

BUY & SAVE
$56.99
Software Design by Example
4 SCHLAGE F40 CSV ORB 626 Commercial Series Orbit Door Knob, Bed & Bath Privacy Lock, Satin Chrome (Pack of 2)

SCHLAGE F40 CSV ORB 626 Commercial Series Orbit Door Knob, Bed & Bath Privacy Lock, Satin Chrome (Pack of 2)

  • PRIVACY LOCK: PERFECT FOR BEDROOM AND BATHROOM DOORS; EASY EMERGENCY ACCESS.

  • QUICK DIY INSTALLATION: HASSLE-FREE INSTALL WITH JUST A SCREWDRIVER; FITS ANY DOOR.

  • DURABLE QUALITY: PREMIUM METAL CONSTRUCTION; TRUSTED LIFETIME WARRANTY INCLUDED.

BUY & SAVE
$45.58 $55.98
Save 19%
SCHLAGE F40 CSV ORB 626 Commercial Series Orbit Door Knob, Bed & Bath Privacy Lock, Satin Chrome (Pack of 2)
5 Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

BUY & SAVE
$23.85
Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools
6 Schlage F40 CSV ELA 626 Commercial Series Elan Door Lever, Bed & Bath Privacy Lock, Satin Chrome

Schlage F40 CSV ELA 626 Commercial Series Elan Door Lever, Bed & Bath Privacy Lock, Satin Chrome

  • SECURE PRIVACY: IDEAL FOR BEDROOMS & BATHROOMS WITH EASY EMERGENCY ACCESS.
  • EFFORTLESS INSTALLATION: SELF-ALIGNING DESIGN INSTALLS IN MINUTES WITH A SCREWDRIVER.
  • DURABLE ASSURANCE: GRADE 2/AAA CERTIFIED FOR TOP-NOTCH SECURITY AND LONGEVITY.
BUY & SAVE
$40.81
Schlage F40 CSV ELA 626 Commercial Series Elan Door Lever, Bed & Bath Privacy Lock, Satin Chrome
+
ONE MORE?

To combine multiple CSV files in PHP, you can follow these steps:

  1. Open a new CSV file in write mode using the fopen function and set the mode to append the content (a+). $combinedFile = fopen('combined.csv', 'a+');
  2. Iterate through each CSV file that you want to combine using a loop. $filePaths = ['file1.csv', 'file2.csv', 'file3.csv']; foreach ($filePaths as $filePath) { // Read the content of the CSV file $file = fopen($filePath, 'r'); // Iterate through each line of the file while (($line = fgetcsv($file)) !== false) { // Write the line into the combined CSV file fputcsv($combinedFile, $line); } // Close the file fclose($file); }
  3. Finally, close the combined CSV file. fclose($combinedFile);

By following these steps, you will be able to combine multiple CSV files into one using PHP.

What is the easiest way to combine multiple CSV files in PHP?

The easiest way to combine multiple CSV files in PHP is by using the fopen() and fwrite() functions to read data from each source file and write it into a single target file. Here's an example:

$files = ['file1.csv', 'file2.csv', 'file3.csv']; $targetFile = 'combined.csv';

$fp = fopen($targetFile, 'w');

foreach ($files as $file) { if (($handle = fopen($file, 'r')) !== false) { while (($data = fgetcsv($handle)) !== false) { fputcsv($fp, $data); } fclose($handle); } }

fclose($fp);

In this example, an array $files is created with the names of the CSV files to be combined. The variable $targetFile stores the name of the output file.

The code uses a foreach loop to iterate over each file. It then opens the file using fopen() in read mode ('r'). It uses a while loop to read each row of the CSV file using fgetcsv() and writes it to the target file using fputcsv(). Finally, the source file is closed with fclose().

After all the files have been processed, the target file is closed with fclose(). The resulting file will contain the combined data from all the CSV files.

How do I merge CSV files into one using PHP?

To merge multiple CSV files into one using PHP, you can follow these steps:

  1. Define an array to store the file paths of the CSV files you want to merge.

$csvFiles = [ 'file1.csv', 'file2.csv', 'file3.csv' ];

  1. Create an output file to store the merged data.

$outputFile = 'merged.csv'; $handle = fopen($outputFile, 'w');

  1. Iterate over each CSV file, open it, and copy its content into the output file.

foreach ($csvFiles as $csvFile) { $handleCsv = fopen($csvFile, 'r');

// Skip the header row in subsequent files (optional)
if ($csvFile !== $csvFiles\[0\]) {
    fgetcsv($handleCsv);
}

while (($data = fgetcsv($handleCsv)) !== false) {
    fputcsv($handle, $data);
}

fclose($handleCsv);

}

  1. Close the output file.

fclose($handle);

Once you have implemented these steps, the CSV files specified in the $csvFiles array will be merged into one file called merged.csv.

Note: Make sure the PHP script has write permissions in the folder where the merged file is being created.

What PHP function can I use to merge multiple CSV files?

You can use the fputcsv() function in PHP to merge multiple CSV files. Here's an example of how you can do it:

function mergeCSVFiles($outputFile, $inputFiles) { $output = fopen($outputFile, 'w');

foreach ($inputFiles as $file) { $input = fopen($file, 'r');

while (($data = fgetcsv($input)) !== false) {
  fputcsv($output, $data);
}

fclose($input);

}

fclose($output); }

// Usage $inputFiles = [ 'file1.csv', 'file2.csv', 'file3.csv' ];

mergeCSVFiles('output.csv', $inputFiles);

In this example, the mergeCSVFiles() function takes the output file name and an array of input file names as parameters. It then opens each input file one by one, reads the data row by row using fgetcsv(), and writes the data into the output file using fputcsv(). Finally, it closes all the file handles.

How can I merge CSV files with different delimiters in PHP?

To merge CSV files with different delimiters in PHP, you can follow these steps:

  1. Initialize an array to store the merged data.

$mergedData = [];

  1. Read each CSV file and store their content in the array.

$csvFiles = ["file1.csv", "file2.csv", "file3.csv"];

foreach ($csvFiles as $file) { $delimiter = detectDelimiter($file); // Function to detect delimiter based on file content

if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
        $mergedData\[\] = $data;
    }
    fclose($handle);
}

}

  1. Write the merged data to a new CSV file.

$mergedFile = "merged.csv";

if (($handle = fopen($mergedFile, "w")) !== FALSE) { foreach ($mergedData as $data) { fputcsv($handle, $data); } fclose($handle); }

Note: The detectDelimiter() function is not specified in the code snippet above. You can implement it based on your needs, such as checking the content of each file to determine the delimiter used.