Image upload error: Undefined index [duplicate]

2

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

Field in 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 anyone help me?

    
asked by anonymous 02.03.2015 / 23:53

1 answer

1

You should check that $_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);
}

Do not forget to use the enctype attribute with the value multipart/form-data when uploading 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-data :

  

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

    
03.03.2015 / 00:33