Image upload error: Undefined index

Asked

Viewed 251 times

2

I have a problem trying to upload an image by PHP.

Field on the form:

<input type="file" name="imagem" id="ff_imagem_serv"/>

Then when I give Submit it executes the code:

$name = $_FILES['imagem']['name'];
$tmp_name = $_FILES['imagem']['tmp_name'];

$location = "imagens_noticia/$name";
move_uploaded_file($tmp_name,$location);

Can someone help me?

  • 1

    What’s the matter?

  • Does not work. From the first two lines error $name = $_FILES['image']['name']; $tmp_name = $_FILES['image']['tmp_name'];

  • Error: Undefined index: image

1 answer

1


You should check whether $_FILES['imagem'] exists with the function isset.

if (isset($_FILES['imagem'])){
    $name = $_FILES['imagem']['name'];
    $tmp_name = $_FILES['imagem']['tmp_name'];
    $location = "imagens_noticia/$name";

    move_uploaded_file($tmp_name,$location);
}

Don’t forget to use the attribute enctype with the value multipart/form-data when sending files, it is used to specify how the form data should be encoded when sent to the server.

<form action="#" method="post" enctype="multipart/form-data">

w3Schools - Multipart/form-date:

Characters are not encoded. This value is required when you are using forms that have a file upload control.

  • 1

    You saved my life! Thank you very much!

Browser other questions tagged

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