如何使一个上载脚本在php mysqli的csv格式的文件,并在其中有一个内容验证


How to make an upload script in php mysqli of a csv format file and has a content validation in it?

这是我的脚本上传部分,我如何添加csv上传的内容验证?有人能帮忙吗?

<?php
    ini_set('upload_max_filesize', '4M');

    $filename = $_POST['filename'];
    if(!isset($_FILES['filename'])){
        echo "No file selected";
    } 
    else{
        $error=array();
        $extension=array("xlsx","csv");
        foreach($_FILES["filename"]["tmp_name"] as $key=>$tmp_name)
        {
            echo "<br/>Filename: ".$file_name=$_FILES["filename"]["name"][$key];
            echo "<br/>Temporary: ".$file_tmp=$_FILES["filename"]["tmp_name"][$key];
            echo "<br/>Size: ".$size=$_FILES["filename"]["size"][$key];
            echo "<br/>Extension: ".$ext=pathinfo($file_name, PATHINFO_EXTENSION);
            if(in_array($ext,$extension))
            {
                    if(!file_exists("/var/www/html/phpsample/".$file_name)){
                        if(move_uploaded_file($file_tmp=$_FILES["filename"]["tmp_name"][$key], "/var/www/html/phpsample/".$file_name)){
                               //echo "Success on File Not Exist";
                             $insert = insertFile($file_name);
                               if($insert){
                                    header("location: index.php?success=true");
                               }
                                else{
                                    echo mysql_error();
                                }
                        //echo "File Not Exist";
                        //echo "<br/>Error: ".$error = $_FILES["filename"]["error"][$key];
                        //echo "<br/>php info: ".phpinfo();
                        }
                    }
                    else
                        {
                            $filename=basename($file_name,$ext);
                            $newFileName=$filename.time().".".$ext;
                            if(move_uploaded_file($file_tmp=$_FILES["filename"]["tmp_name"][$key], "/var/www/html/phpsample/".$newFileName)){
                                //echo "Success on File Exist";
                                $insert = insertFile($newFileName);
                                if($insert){
                                    header("location: index.php?success=true");
                                }
                            }
                            //echo "File Exist";
                        }
               //echo "Extension of file is in our array list";
            }
            else{
                //array_push($error,"$file_name, ");
                echo "The file you upload is not included in allowed file extension list.";
            }
        }   
    }
?>

按如下方式使用fgetcsv()读取文件,并在循环中进行验证。

请参阅下面的示例代码。

$fileData=fopen($filename, 'r');
while(!feof($fileData))
{
   //do validation for fgetcsv($file) value here
  if($i%5 == 0){
       if(is_numeric(fgetcsv($file)) && strlen(fgetcsv($file)) == 4){
           return true;
       }
       else{
             return false;
       }
   } 
   $i++;
   print_r(fgetcsv($file));
}