CSV File Importer PHP

Purpose

The CSV File Importer imports one or more CSV files from a local Windows folder into an existing MySQL database.

The PHP file reads the CSV files directly from the Windows file system. It does not upload the files through the web browser. This method avoids normal PHP file-upload size limits and reduces memory problems when large CSV files are processed.

For each CSV file, the importer:

•Creates a MySQL table.

•Uses the CSV filename as the table name.

•Uses the first row of the CSV file as the column names.

•Cleans the column names so that they are suitable for MySQL.

•Imports the CSV data one row at a time.

•Checks for rows that do not contain the expected number of columns.

•Calculates the maximum character length used in each column.

•Changes each suitable column from TEXT to the smallest required VARCHAR(x) size.

•Displays an import summary when processing is complete.

Selecting the Database and CSV Files

The user enters:

•The destination MySQL database name.

•The full Windows folder path that contains the CSV files.

•The CSV file to import.

The importer scans the selected Windows folder and automatically displays the available .csv files. The user can select one CSV file or select All CSV files to process all CSV files in the folder.

The CSV files remain in their original Windows folder. They are not uploaded through the browser.

Database Connection

The importer connects to the local MySQL server and selects the database entered by the user.

The database must already exist. The importer creates the required tables, but it does not create the database.

The MySQL connection uses the utf8mb4 character set and the utf8mb4_0900_ai_ci collation.

Table Creation

Each CSV file creates one MySQL table.

The CSV filename, without the .csv extension, becomes the MySQL table name.

For example:

diagnostics.csv

creates:

diagnostics

If a table with the same name already exists, the importer deletes the existing table and creates a new table.

Delimiter Detection

The importer reads the first line of the CSV file and checks the number of commas and semicolons.

It then selects either:

•comma ,, or

•semicolon ;

as the CSV delimiter.

This permits the importer to process CSV files that use either of these common formats.

Column Name Processing

The first CSV row is used as the source of the MySQL column names.

Before the table is created, the importer cleans each column name.

The cleaning process:

•Removes leading and trailing spaces.

•Removes a UTF-8 BOM if present.

•Removes full stops.

•Replaces unsupported characters with underscores.

•Replaces spaces with underscores.

•Removes repeated underscores.

•Removes underscores from the start and end of the name.

For example:

Hospital Code.

becomes:

Hospital_Code

This process creates column names that are more suitable for use in MySQL queries.

Initial Column Types

When the MySQL table is first created, all CSV columns use:

TEXT NULL

Using TEXT during the initial import prevents the importer from having to know the final length of each column before the data is loaded.

Row Import

The importer reads the CSV file one row at a time.

This method is suitable for large CSV files because the complete file is not loaded into PHP memory.

Empty rows are skipped.

Before a row is imported, the importer checks that the number of values in the row matches the number of CSV columns.

If the number does not match, the importer skips the row and displays:

•The row number.

•The expected number of columns.

•The actual number of columns.

•The detected delimiter.

•The contents of the row.

This information helps identify incorrect or damaged CSV data.

Valid rows are inserted into the MySQL table.

Automatic Column Size Optimisation

After the CSV data is imported, the PHP file optimises the MySQL column sizes.

The importer checks the maximum number of characters stored in each column by using:

MAX(CHAR_LENGTH(column))

All column maximum lengths are calculated in one scan of the table. CHAR_LENGTH() is used because it measures characters rather than bytes. This is important when utf8mb4 data is used.

For example, if the longest value in a column contains 27 characters, the column is changed from:

TEXT

to:

VARCHAR(27)

An empty column uses a minimum size of:

VARCHAR(1)

This prevents an invalid VARCHAR(0) definition.

Large Columns

A utf8mb4 character can require up to four bytes.

If a column contains a value longer than 16,383 characters, the importer does not convert that column to VARCHAR.

The column remains as TEXT.

This prevents the conversion from exceeding the practical VARCHAR size used by the importer.

If MySQL cannot convert a column because of the maximum row-size limit or another database restriction, the importer also leaves that column as TEXT and displays the MySQL error.

Processing Multiple Files

If All CSV files is selected, the importer processes each CSV file separately.

Each file creates its own MySQL table.

Files are processed one at a time. This reduces memory use and permits large groups of CSV files to be imported without loading all files into memory at the same time.

Import Results

After each CSV file is processed, the importer displays a summary that includes:

•Number of rows imported.

•Number of rows skipped.

•Number of columns imported.

•Number of columns skipped.

•MySQL table name.

•Imported column names.

•Number of columns converted to VARCHAR.

•Number of columns that remain as TEXT.

When all selected files are complete, the importer also displays:

•Script start time.

•Script end time.

•Total processing time.

Summary

The CSV File Importer provides an automated method to convert local CSV files into MySQL tables.

It creates the table structure from the CSV header, imports the data row by row, reports incorrect rows, and then optimises each column according to the actual data stored in that column.

The use of direct local file access and row-by-row processing makes the importer suitable for CSV files that are too large for normal browser-based PHP uploads.


Topic last reviewed: 19/10/26