Error in PHP image upload

Asked

Viewed 169 times

2

Blz Galera

I am doing an image upload test and is showing error:

Notice: Undefined index: foto in C: xampp htdocs cadastro.php on line 2

Notice: Undefined index: foto in C: xampp htdocs cadastro.php on line 3

What’s wrong? Thanks in advance

Note. The script in index.php is because I have more than one button in the form.

index php.

<form id="cadastro" name="cadastro" method="post">
<input type="file" name="foto" />
<input type="button" value="Cadastrar" onClick="ExecutaAcao('cadastro');">
</form>

<script>
function ExecutaAcao(valor){
document.cadastro.action = valor + '.php';
document.cadastro.submit();
}
</script>

php.

<?php 
$imagem = $_FILES['foto']['name'];
$tmp_name = $_FILES['foto']['tmp_name'];
$location = "imagens/$imagem";
move_uploaded_file($tmp_name,$location);
?>
  • Put an id in your input, maybe that’s -> <input type="file" id="photo" name="photo" />

1 answer

0


Add the enctype in your form:

<form id="cadastro" name="cadastro" method="post" enctype="multipart/form-data">

Read more here and here.

Browser other questions tagged

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