Development
How to Import a Large CSV File Into MySQL With PHP

A large CSV import should stream rows rather than load the entire file into memory. Validate the header, parse and validate one record at a time, write bounded batches, and record enough progress to restart without duplicating completed work.
Memory use, database contention, and restart behavior matter more than a single clever insert statement. Start with a staged import that can be reviewed before it changes production records.
Stream the CSV file with PHP
For PHP 8.1 or newer, use fgetcsv with explicit delimiter, enclosure, and escape behavior. The empty escape argument avoids relying on the historical backslash behavior.
This reader expects a UTF-8 file with email,name as the exact header. It yields source record numbers and normalized fields, leaving persistence to a separate importer.
<?php
declare(strict_types=1);
/** Yield validated contact records without loading the complete CSV. */
function readContacts(string $path): Generator
{
$handle = fopen($path, 'rb');
if ($handle === false) {
throw new RuntimeException('Cannot open import file.');
}
try {
$header = fgetcsv($handle, null, ',', '"', '');
if ($header !== ['email', 'name']) {
throw new RuntimeException('Expected email,name header.');
}
$record = 1;
while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
$record++;
if ($row === [null]) {
continue;
}
if (count($row) !== 2) {
throw new RuntimeException('Invalid columns at record ' . $record);
}
[$email, $name] = array_map('trim', $row);
if (strlen($email) > 254 || !filter_var($email, FILTER_VALIDATE_EMAIL)
|| $name === '' || strlen($name) > 200) {
throw new RuntimeException('Invalid values at record ' . $record);
}
yield ['record' => $record, 'email' => $email, 'name' => $name];
}
if (!feof($handle)) {
throw new RuntimeException('CSV read failed.');
}
} finally {
fclose($handle);
}
}
The count is a CSV record number, not a physical line number: quoted fields can contain newlines. PHP's fgetcsv documentation describes parsing behavior. The sample fails at the first invalid row; a production staging importer may instead record errors and continue under a defined policy.
Bound the input contract
Set a maximum file size, maximum expected field sizes, allowed encoding, and expected column count before import. Streaming the file does not bound one enormous record: fgetcsv still needs to allocate the record it parses. Constrain untrusted uploads and run the worker with appropriate resource limits.
Handle a UTF-8 byte-order mark deliberately if your data source emits one. Do not silently guess encodings or truncate columns until they fit. Reject or transform under an explicit, reviewable rule.
Write bounded batches
Prepare SQL once and reuse it. Commit a limited number of rows per transaction, choosing the batch size from measurement on representative data. One transaction for a huge file can hold locks and accumulate undo work for an unnecessarily long time.
Define duplicate policy before writing: reject, skip, update selected fields, or create a separate exception record. A generic upsert can overwrite useful production data if the source is stale. Enforce the actual business uniqueness rule in MySQL.
Use staging tables when the import needs cross-row validation, a dry-run report, or human review. Import success should mean more than “the parser reached EOF.” Reconcile accepted, rejected, skipped, and applied counts.
Make restart behavior explicit
Assign the import a stable identifier and source checksum. Store a checkpoint only after the associated batch commits. On restart, verify the checksum and resume from a durable boundary, using unique source-record identities to prevent reapplying completed rows.
A raw byte offset requires care because the file must be unchanged and the parser must resume at a record boundary. A simpler initial implementation can replay earlier records and skip those already committed, trading time for clarity.
For very large trusted datasets, MySQL's LOAD DATA documentation describes a bulk-loading alternative. Review its file-access and LOCAL configuration implications rather than enabling it casually.
Test the ugly files
Include quoted commas, embedded newlines, blank records, duplicate keys, malformed columns, invalid encoding, oversized fields, and termination between batches. Compare final counts and representative records after restart.
For the larger job boundary, see incremental PHP refactoring. Discuss an import workflow review when data cleanup has become part of every run.