Problem sending a file by POST

Asked

Viewed 50 times

0

The following code that I present to you right below is to upload a file ". CSV" and send the data to Mysql, but the post method is not sending anything to the function . PHP, and I get the message "empty".

<!DOCTYPE html>

Upload Stock

<form action="" method="POST" enctype="multipart/form-data">
    <div align="center">
        <p>Upload CSV: <input type="file" name="file"></p>
        <p><input type="submit" name="submit" value="Importar"></p>
    </div>

</form>

<?php
    include "APIConnecting.php";

    if (isset($_POST["submit"])) {
        if ($_FILES['file']['name']) {
            $arquivo = explode(";", $_FILES['file']['name']);
            if ($arquivo[1] == "csv") {
                $handle = fopen($_FILES['file']['tmp_name'], "r");
                while ($dados = fgetcsv($handle, 50)){
                    $item1 = mysqli_real_escape_string($con, $dados[0]);
                    $item2 = mysqli_real_escape_string($con, $dados[1]);
                    $sql = "INSERT INTO estoque (Chassi, Navio) VALUES ('$item1', '$item2')";
                    mysqli_query($con, $sql);
                }
                fclose($handle);
            }

        }
    }else{echo "vazio";}
?>

1 answer

0

  • At line 4 where you check for the POST you could put the File field to ensure that it is sending a file.
  • In Line 6 you put ; (semicolon) the right one would . (point) to catch extension.

Changing only that, I tested it here and it worked. The Code went like this:

if (isset($_FILES['file'])) { 
    if ($_FILES['file']['name']) {
        $arquivo = explode(".", $_FILES['file']['name']);
        if ($arquivo[1] == "csv") {
            $handle = fopen($_FILES['file']['tmp_name'], "r");
            while ($dados = fgetcsv($handle, 50)){
                $item1 = mysqli_real_escape_string($con, $dados[0]);
                $item2 = mysqli_real_escape_string($con, $dados[1]);
                $sql = "INSERT INTO estoque (Chassi, Navio) VALUES ('$item1', '$item2')";
                if(mysqli_query($con, $sql)){
                  echo "Salvo com Sucesso.";
                }
            }
            fclose($handle);
        }

    }
}else{echo "vazio";}

  • How did your html? here is not working yet, is not going through the first condition. It is returning empty.

Browser other questions tagged

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