how to validate a quantity of columns from a csv file in php?

1

I'm doing upload with a load data infile in php. In this upload you would need to do a validation of the number of columns that have the .csv file. If the amount is different from the amount that comes from the bank it blocks the load for the bank.

My code is below:

//aqui é o caminho da pasta para onde o arquivo irá
$target_dir = "uploads/";

//aqui pega o arquivo no campo para fazer o upload
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
//pega a extensão
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

$type = $_REQUEST["typeFile"];

if ($uploadOk == 0) {
    echo "<script>alert('Desculpe, seu arquivo não foi enviado');location.href='upload.php';</script>";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

    //echo $type."<br>";
    $arr = explode(".",$campo);
    $week = $arr[0];
    //echo $week;
    $block = substr($week,0,4); 
    echo $block;

 if($block == "WEEK" || $block == "week" || $block == "Week"){

    $con = mysql_connect("localhost","root","") or die("erro1");
    $db = mysql_select_db("banco") or die("erro2");

    $delete = mysql_query("delete from reparo.producao where week = '$week'");

    $sql = "LOAD DATA LOCAL INFILE 'c:/inetpub/wwwroot/report/uploads/$campo'
                            INTO TABLE reparo.producao
                            FIELDS TERMINATED BY ','
                            ENCLOSED BY '\"'
                            LINES TERMINATED BY '\r\n'
                            IGNORE 1 LINES
                            (@data_producao,@part_number,@total_producao,@week)
                            SET
                            'data_producao' = trim(@data_producao),
                            'part_number' = trim(@part_number),
                            'total_producao' = trim(@total_producao),
                            'week' = '$week'";

    mysql_query($sql)or die("erro3");   

}else{
    echo "<script>alert('Desculpe, renomeie o arquivo por exemplo week-5-16');location.href='upload.php';</script>";
}
        echo "<script>alert('enviado com sucesso!');location.href='upload.php';</script>";
    } else {
        echo "<script>alert('Desculpe, houve um erro ao enviar o arquivo');location.href='upload.php';</script>";
    }
}
Can anyone help me with how to do this column count of the file that will do the upload ?

    
asked by anonymous 13.07.2016 / 16:31

0 answers