how to validate a number of columns of a csv file in php?

Asked

Viewed 353 times

1

I’m making upload with a load data infile in php. Nesse upload would need to validate the amount of columns that have the file .csv. If the amount is different from the amount coming from the bank it blocks the load to 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 how to do this column count of the file that will do the upload?

  • csv already comes with columns in the first row?

  • yes, but do not insert in the bank just would like to check the amount of columns

  • 1

    Take this line, from a explode() and a count() to know the number.

  • rray did not get your answer right, you talk to open the file and make a comma explode

  • That’s right, you can fopen() and read only the first line, I imagine it contains the header.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.