php - Validate Headers ON CSV & TXT -
I have a basic import tool that we are using to upload to our database using PHP. Here is a sample script. My question is how do I accept headers before importing? Actually check against a price to ensure that the correct file is being imported. I see everywhere online, but my headers can not get the answer
SKU, value, active
LOAD data LOW_PRIORITY LOCAL INFILE '$ file' table In
sample
. "Optionally closed" \ "lines suspended" by theyour_temp_table
field '\ N' IGNORE 1 LINES // Continue to run the rest of the script
How to validate the header before running my query script? I
$ file = $ _GET ['file']; $ file = urldecode ($ file); / / File is being uploaded on
I think you'll get a $ file The first line of
will be required to read and The comparison header will be from a specified list of headers:
$ required header = array ('SKU', 'value', 'active'); // headers we $ f = fopen ($ file , 'R'); $ firstLin expects e = fgets ($ f); // gets the first line of the CSV file fclose ($ f) $ foundHeaders = str_getcsv (trim ($ first line), ',', '' '); // Pars the array if ($ foundHeaders! == $ required header) {echo' Headers do not match: '.mempload (', ', $ found header); die ();} / / Run Import Script ...
Comments
Post a Comment