PHP Form / Upload file and send other fields at once

0

How to upload a file and send the other form data in a single action?

I have (as an example) the following form:

<form method="POST" name="form" action="ccd.php">
    <input value="" type="text" id="nome" name="nome"/>
    <input value="" type="text" id="modelo" name="modelo"/>
    <input type="file" id="foto" name="foto"/>
</form>

But when I add the enctype from "multipart / form-data" to pass the data from the file to ccd.php , it does not send < in and template .

<form enctype="multipart/form-data" method="POST" name="form">

When I remove the enctype from "multipart / form-data", it passes the name and template data, but file data.

$uploaddir = "../fotos";

if (isset($_POST['foto'])){
    $uploadfile = $uploaddir . basename($_FILES['foto']['name']);
    if(move_uploaded_file($_FILES['foto']['tmp_name'], $uploadfile){}
}

And I get the error:

Notice: Undefined index: foto in /media/[...]/ccd.php on line 31
    
asked by anonymous 28.02.2018 / 15:48

1 answer

1

I noticed that a parenthesis is missing on this line

if(move_uploaded_file($_FILES['foto']['tmp_name'], $uploadfile)) {}

In addition, the contents of the file are placed in the $_FILES array. How do you if (isset($_POST['foto'])){ will never execute.

The enctype="multipart/form-data" is required in this case.

    
28.02.2018 / 16:33