验证CSV和TXT上的标题

Ant*_*lli 1 php mysql validation

我有一个基本的导入工具,我们用它来使用PHP上传到我们的数据库.这是一个示例脚本.我的问题是如何在导入之前验证标题?基本上检查一个值,以确保正确的文件被导入.我在网上到处看,但似乎找不到答案我的标题是

SKU,价格,有效

LOAD DATA LOW_PRIORITY LOCAL INFILE'$ file'INTO TABLE sample.your_temp_table字段被'''''''''''''''''''''''''''''''''''''''''

我的问题是如何在脚本运行之前验证标头?我正在使用

$ file = $ _GET ['file']; $ file = urldecode($ file); //上传文件

cOl*_*le2 8

我想你需要阅读第一行$file并将其与一组标题进行比较:

$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…
Run Code Online (Sandbox Code Playgroud)