I have a basic import tool that we are using to upload into our database using PHP. Here is a sample script. My question is how can I validate the headers before import? Basically check against a value to make sure the right file is getting imported.I looked everywhere online, but can't seem to find the answer My headers are
SKU, Price, Active
LOAD DATA LOW_PRIORITY LOCAL INFILE '$file' INTO TABLE
sample
.your_temp_table
FIELDS TERMINATED BY ' ' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY ' ' IGNORE 1 LINES //Continue to run rest of script
My question is how to validate the headers before the script is ran? I'm using
$file = $_GET['file']; $file = urldecode ($file); //getting file on upload
I think you will need to read the first line of the $file
and compare it to a set list of headers:
$requiredHeaders = array('SKU', 'Price', 'Active'); //headers we expect
$f = fopen($file, 'r');
$firstLine = fgets($f); //get first line of csv file
fclose($f); // close file
$foundHeaders = str_getcsv(trim($firstLine), ',', '"'); //parse to array
if ($foundHeaders !== $requiredHeaders) {
echo 'Headers do not match: '.implode(', ', $foundHeaders);
die();
}
//run import script…