我有此脚本能够读取正在完全运行的csv文件,但现在我想先对其进行验证。上传之前,我想检查该行上的每个数据。如果数据在行上发现为空,则将引发错误。我尝试了很多验证,但似乎缺少。
这是我上传的脚本:
if(!empty($_FILES["charge_file"]["name"])){
$file = explode('.', $_FILES['charge_file']['name']);
$csvfile = end($file);
$csvfile_ext = array('csv');
if(in_array($csvfile, $csvfile_ext)){
$handle = fopen($_FILES["charge_file"]["tmp_name"],"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$chrg_empid = $data[0];
$chrg_empname = $data[1];
$chrgemp_name = utf8_encode($chrg_empname);
$chrg_cvf = $data[2];
$chrg_amount = $data[3];
$chrg_store = $data[4];
$chrg_date = $data[5];
$chrg_installment = $data[6];
$chrg_ded_type = $data[7];
charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment);
}
// echo "success";
fclose($handle);
}
else{
echo 'invalid';
}
}
else{
echo 'select';
}
这是我插入的函数:
function charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment){
$db = database2();
$query = "INSERT INTO CFV_ENTRY (EMP_NO, EMP_NAME, REF_NO, AMOUNT, STORES, TRXN_DATE, DED_TYPE, NO_INSTALLMENT, DATE_INSERTED, ENTRY_TYPE, ENTRY_DESC) VALUES ('$chrg_empid', '$chrg_empname', '$chrg_cvf', '$chrg_amount', '$chrg_store', '$ent_date', '$chrg_ded_type', '$chrg_installment', SYSDATE, '1', 'CHARGE')";
$cmd = $db->prepare($query);
$cmd->execute();
$db = null;
}
这是我的验证:
function isEmptyFields($csv_file){
$handle = fopen($csv_file,"r");
$counter = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$counter = $counter + 1;
if ($data[0] != null) {
if ($data[1] != null) {
if($data[2] != null){
if ($data[3] != null) {
if($data[4] != null){
if($data[5] != null){
if($data[6] != null){
if($data[7] != null){
// echo $chrg_empid = $data[0];
echo 'success';
// echo $chrg_empid = $data[0]."\n";
// $chrg_empname = $data[1]."\n";
// echo $chrgemp_name = utf8_encode($chrg_empname)."\n";
// echo $chrg_cvf = $data[2]."\n";
// echo $chrg_amount = $data[3]."\n";
// echo $chrg_store = $data[4]."\n";
// echo $chrg_date = $data[5]."\n";
// echo $chrg_installment = $data[6]."\n";
// echo $chrg_ded_type = $data[7]."\n";
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}else{
echo 'Empty field is in line: '.$counter;
}
}
// fclose($handle);
// return $handle;
}
答案 0 :(得分:0)
这是一个遍历该行并检查元素是否为空的函数。
function hasEmptyField(array $data)
{
for ($i = 0; $i <= 7; $i++) {
// Make sure that the key exists, isn't null or an empty string
if (!isset($data[$i]) || $data[$i] === '') {
return true;
}
}
return false;
}
您可以在while
循环中使用该功能:
$counter = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$counter++;
if (hasEmptyField($data)) {
// It has an empty field. Echo an error and skip to next row
echo 'Empty field is in line: ' . $counter;
continue;
}
$chrg_empid = $data[0];
$chrg_empname = $data[1];
$chrgemp_name = utf8_encode($chrg_empname);
$chrg_cvf = $data[2];
$chrg_amount = $data[3];
$chrg_store = $data[4];
$chrg_date = $data[5];
$chrg_installment = $data[6];
$chrg_ded_type = $data[7];
charges_individual_entry($chrg_empid, $chrg_empname, $chrg_cvf, $chrg_amount, $chrg_store, $ent_date, $chrg_ded_type, $chrg_installment);
}
答案 1 :(得分:0)
尝试以下-
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$counter++;
foreach($data as $entry) {
if(!$entry) {
echo "Error on line $counter";
break;
}
}
//other code goes here
}
答案 2 :(得分:0)
后续检查任何空行并跳过它
$file_path="path/to/csvfile.csv";
if (($handle = fopen($file_path , "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (array(null) !== $data) {
//process lines which are not blank
}
}
}