Print meshing when csv file uploaded is larger or smaller than 9 columns

Asked

Viewed 101 times

1

My application reads a csv file and convert to pdf. For this I have the frontend in html to load the file. I would like to know how to show the user a message stating that the file is invalid. The file will be invalid when the number of columns of the csv file loaded 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>
  • Have you tried inside the server side to count the columns of the file and if they are in your requirement equal 9 pass conversion to pdf?

1 answer

1

I believe csv files indexed as follows:

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

On the lines he names the fields divided by ; (point and comma), then all you need to do is put the first row inside an array and count the lenght of that array so you have the number of columns there you can check if it is the same equal to 9 if it is true convert if it does not return invalid message

NOTE: There must be an easier way to check columns / fields in csv files, I’m just giving a hint of what can be done

Browser other questions tagged

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