How do I check if a $_FILE has been set?

Asked

Viewed 31 times

1

all right?

I’m trying to run this code:

if(empty($_FILES['imagem']['name'])){
    $upload = new Upload();
    $novoNome = $upload->efetuarUpload($_FILES["imagem"]);
    $pontoTrocaAlterado->setImagem($novoNome);
}

I need that only when the user uploads the file, he perform the Upload function and set the new image name. However doing the tests, even when the user does not upload the file, the name is changed.

I’ve used several functions like:

in_array array_key_exists

But I didn’t succeed, someone could help me?

  • Name of which file changes when not uploaded?

  • Image field, in the database

  • isset() does not solve your case?

  • In fact, there was one ! before the empty, to get into the if only when the value is not empty?

  • I managed to solve, as it was a file change case I did the following: if(!empty($_FILES['imagem']['name'])){
 $upload = new Upload();
 $novoNome = $upload->efetuarUpload($_FILES["imagem"]);
 $pontoTrocaAlterado->setImagem($novoNome);
 }else{
 $pontoTrocaAlterado->setImagem($imagem);
 }. @Andersoncarloswoss I followed your suggestion to add ! , but I noticed that even if I didn’t define the name it didn’t write in the bank, because the bank doesn’t accept null, then I saved the old name of the file.

1 answer

2

I managed to solve, as it was a case of alteration of the file I did the following:

    if(!empty($_FILES['imagem']['name'])){
    $upload = new Upload();
    $novoNome = $upload->efetuarUpload($_FILES["imagem"]);
    $bannerAlterado->setImagem($novoNome);
}else{
    $bannerAlterado->setImagem($imagem);
}   

I followed @Andersoncarloswoss' suggestion to add ! , but I noticed that even if I didn’t define the name it didn’t write in the bank, because the bank doesn’t accept null, then I saved the old name of the file.

Browser other questions tagged

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