2016-11-25 18 views
0

FILE.CSV結構:如何驗證PHP中的CSV列?

file.csv

之前上傳到文件夾上傳,我要檢查的列。

的算法是這樣的:

if ((column 1 = 'no') && (column 2 = 'product_name') && (column 3 = 'product_price') && (column 4 = 'qty')) 
{ 
    upload(); 
} 
else 
{ 
    die; 
} 

有沒有這種情況下使用PHP代碼或SQL驗證任何編碼?

回答

0

我們必須在位置加載csv文件並獲取csv文件內容並驗證內容。

這是我的例子:

$destination=C:\Users\AIT\Desktop\SC MArgins new.csv; 
$contents = file ($destination); 
for($i=1; $i<sizeof($contents); $i++) 
{ 
$line = trim($contents[$i],'",'); 
$arr = explode(',',$line); 
if($arr[0]=='') 
      { 
       $rows.='Item#,'; 
      } 

      if($arr[1]=='') 
      { 
      $rows.='Product Name,'; 
      } 

      if($arr[2]=='') 
      { 
      $rows.='Product Code,'; 
      } 

      if($arr[3]=='') 
      { 

      $rows.='Product Grade,'; 

      } 
if($rows!=''){ 
      $row1.='Row '.$i.'-'. $rows.'can not be empty'."<br>";} 
      $k.=$row1; 
      } 
if($k!=''){   
      $common->setMsg($row1, 'box_warning'); 
      return false; 

}

此代碼將幫助ü得到缺少哪一列。按照此代碼,讓我知道是否有任何問題處理csv