PHP Form / File upload and upload other fields at once

Asked

Viewed 103 times

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 the ccd.php, it does not send the data of name and model.

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

When I remove the enctype of "Multipart/form-data", it passes the data of name and model, but does not pass the 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 mistake:

Notice: Undefined index: foto in /media/[...]/ccd.php on line 31
  • something that is in the question does not match the reality has to use the enctype,But, you look at question, What do you say you don’t carry? What is line 31? missing supplement your question !!!

1 answer

1


I noticed that there is one parenthesis left on this line

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

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

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

Browser other questions tagged

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