Print message when loaded csv file is larger or smaller than 9 columns

1

My application reads a csv file and convert it to pdf. For this I have the frontend in html to load the file. I would like to know how do I show the user a message stating that the file is invalid. The file will be invalid when the number of columns in the loaded csv file is greater or less than 9.

<!DOCTYPE html>
<html lang="en">
<head>
    </style>
    <meta charset="UTF-8">
    <title>Site Report</title>

</head>
<body>

<h3> Site Report </h3>
</span></span>

<form action="" method="post" enctype="multipart/form-data">
    <labeL>
    <span>Choose CSV file:</span>
    </span></span>
    <input type="file" accept="text/csv" name="userfile"/>
    </labeL>
    <p> </p>
    <input type="submit" name="action" value="Upload"/>

</form>

    <?php 
    if(isset($_POST['action']) && $_POST['action']=='Upload'):
    $userFile=$_FILES['userfile'];
    $name = $userFile['name'];
    $tmp = $userFile ['tmp_name'];



    $extension=explode('.', $name);
    $ext=end($extension);


    $newName =md5($name).'.'.$ext;

    $newName= str_replace(' $name ','$name','uploadFile').'.'.$ext;
    $userMessage="File uploaded successfuly  ";

    if(empty($userFile)):
           echo 'Select an file to upload';
    else:
    if(move_uploaded_file($tmp, 'uploads/'.$newName)):

             echo $userMessage;        
            else:    
            echo 'Error';
            endif;
          endif;
      endif;  

    ?>

<form action="download.php" method="post">
<p>
</p>
<input type="submit" name="submit" value="Download PDF File" />

</form>
</body>
</html>
    
asked by anonymous 03.10.2018 / 16:05

1 answer

1

I believe csv files index as follows:

Email;Nome
[email protected];Teste1
[email protected];Teste2
[email protected];Teste3
[email protected];Teste4

On the lines it puts the field names divided by ; ( semicolon ), so all you have to do is put the first line inside an array and count the lenght of this array so you have the number of columns there you can check if it is equal to 9 if it is true to convert if it does not return invalid message

    
06.10.2018 / 19:51