dqunzip3183 2016-08-11 11:06
浏览 31

在php中验证CSV文件列

Suppose I have three columns email,phoneno and address columns in csv file.I want to upload csv file.At the time of uploading ,I want to validate csv file column name address.For example:- if address column is in csv file have no of addresses and their length is 200 or 300.If address column size is 100 which is mentioned in database if someone exceeds the size of address column in csv file then it gives alert to users that the address field is exceedes its limit. This code is for csv file upload.At the time of upload it checks the csv file column from database.those columns which are match with database show a dropdown with selected columns otherwise there is a dropdown shown with add a field option.

<?php 

   require 'db.php';
// $mimes = array('text/csv');
// if(in_array($_FILES['file']['type'],$mimes)){
  // do something

 if(isset($_POST['upload'])){
if ($_FILES['csv']['size'] > 0) { 


$columnnames='';
$i=0;
$numcolumn='';
$show= '';
$numcolumn1='';
 $columnnames1='';
$j=0;

$file = $_FILES['csv']['tmp_name'];
// if($file["extension"] != "csv")
//   {
//     echo "Please select CSV file";
//     //header("location:ind1.php");
//     exit;
//   }
$target_dir = "uploads/";
$target_file = $target_dir . basename($file);
// $target_file = $target_dir . basename($file);
$handle = fopen($file,"r"); 


$store=$_FILES["csv"]["name"];
 move_uploaded_file($file, "uploads/" . $store);
            //echo "Stored in: " . "uploads/" . $store . "<br />";
$csv_fields = fgetcsv($handle,1000,",",'"'); 


$arr12=array();
$after = array();
$sql = "SHOW COLUMNS FROM user";

 $result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){

$arr12[]=$row['Field'];


}

foreach($arr12 as $v){
    $after[] = implode(' ', array_map('ucfirst', explode('_', $v)));
    // $after1= explode($after);

}
$af=implode(',' , $after);

// /echo $af;

//print_r($after);
//print_r($arr);
$tbl_fields=array_slice($arr12, 1);
$array1 = $csv_fields;
$array2 = $tbl_fields; 

//$c=implode(" ",$array1);
//print_r($c);
//print_r($array2);
$words1 =  array_unique($array1);
//print_r($words1)."<br>";

$words2 =  array_unique($array2);
//print_r($words2)."<br>";
$default=array();
// if (count($array1)>$array2){
//        $error=true;

//    }
$intersection = array_intersect($words1, $words2);

 $intersection1 = array_diff($words1, $words2);

$merge= array_merge($intersection, $intersection1);

usort($merge, function($a, $b) use ($words1){
   // sort using the numeric index of the second array
   $valA = array_search($a, $words1);
   $valB = array_search($b, $words1);

   // move items that don't match to end
   if ($valA === false)
       return -1;
   if ($valB === false)
       return 0;

   if ($valA > $valB)
       return 1;
   if ($valA < $valB)
       return -1;
   return 0;
});
//print_r($merge);
$row = 1;
$f = $handle;


//$tbl_fields = array('first_name','last_name','email');
if ($handle !== FALSE) {

  //if($csv_fields !== FALSE) {
        echo '<div class="container panel" style="background:#F5F7FA;margin-top:30px;">
        <div class="row" >
        <div class="col-md-5" style="float:left;">

                <span class="glyphicon glyphicon-remove btn"></span>';

   echo "<div style='color:red;'>Please check the column headers below to confirm we've labeled them correctly.</div>";

// foreach($merge as $a)
// {
foreach($merge as $a)
{
  if(in_array($a,$array2)){

    // if($columnnames == $a){
       echo "<div class='form-group'><table>"; 

echo "<tr>

 <td><input  type='text' value='".$a."'  class='textbox  form-control' id='textbox'></td>
<td><input  type='hidden' value='".$a."'  class='textbox2  form-control' id='textbox2'></td>

<td><input type='hidden' id='newhiddenBox' class='newhiddenBox' value='".$a."'></td>


<br>"; 

 echo "</tr></table></div>";
 }


else{
  echo "<div class='form-group'><table><tr><td>"; 



    echo "

    <td><input  type='text' value='".$a."'  class='textbox tt form-control' id='textbox'></td>

    <td><input  type='hidden' value='".$a."'  class='textbox1 form-control' id='textbox1'></td>
<td><input type='hidden' id='newhiddenBox'class='newhiddenBox' value='".$a."'></td>  



</tr></table></div>";
}
}

  echo "</div>";
echo "<div class='col-md-7' style='float:left;'>
        <h3>database headers</h3>";
 foreach($merge as $a)
{
  if(in_array($a,$array2)){

    // if($columnnames == $a){
       echo "<div class='form-group'><table>"; 

echo "<tr><td>
<select name='drop[]' class='select_drop textbox textbox1 form-control' id='select_drop' style='width:200px;'>"; 

         echo "<option  value='".$a."'>$a</option>";

         echo "<optgroup label='database headers'>"; 

         $res = mysql_query('select * from user');
$numcolumn = mysql_num_fields($res);

for ( $i = 1; $i < $numcolumn; $i++ ) {
            $columnnames = mysql_field_name($res, $i);

            echo '<option value="'.$columnnames.'" class="d ">'.$columnnames.'</option>';

    // $after1= explode($after);



          } 

         echo "</optgroup></td>";

echo "
<td><input  type='hidden' value='".$a."'  class='textbox2  form-control' id='textbox2'></td>

<td><input type='hidden' id='newhiddenBox' class='newhiddenBox' value='".$a."'></td>
 <td><input type='checkbox'  name='check' value='".$a."' class='check checkbox-primary' >Add Column</td> 

<br>"; 

 echo "</select></tr></table></div>";
 }


else{
  echo "<div class='form-group'><table><tr><td><select name='drop[]' class='drop  textbox  hiddentxtbox form-control' >"; 



        echo "<optgroup selected label='newtextfields'>
        <option value=''>Add Field or Choose</option>
         <option value='text'>Add new Field</option>

         </optgroup>
         <optgroup label='database headers' class='hd'>"; 

         $res = mysql_query('select * from user');
$numcolumn = mysql_num_fields($res);
for ( $i = 1; $i < $numcolumn; $i++ ) {

            $columnnames = mysql_field_name($res, $i);
            echo '<option value="'.$columnnames.'" >'.$columnnames.'</option>';

          } 
         echo "</optgroup>";

     echo "</select></td>";

    echo "


    <td><input  type='hidden' value='".$a."'  class='textbox1 form-control' id='textbox1'></td>
<td><input type='hidden' id='newhiddenBox'class='newhiddenBox' value='".$a."'></td>  


<td><input type='checkbox' name='check'  value='".$a."' class='check checkbox-primary' style='margin-left:5px;'>Add Column</td>

</tr></table></div>";
}
}
       echo  "</div>
        </div>

       <input type='submit' name='submit' id='submit' value='submit' class='col-md-2 btn btn-md btn btn-primary '>

       </div>
        ";


    fclose($handle);

 // }

  }

//}

}
// else{
//   $show="please select a file";
//  }
}


?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 r语言神经网络自变量重要性分析
    • ¥15 基于双目测规则物体尺寸
    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计
    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢