Check if input file is filled

2

I have a difficulty that I can not solve even though it is a simple situation. I'm trying to validate a input field with%, the field needs to be filled, what I have:

Form:

<form enctype="multipart/form-data" class="form-horizontal" id="frmDoc" method="post">

<legend>Selecione o PDF</legend>
<div class="form-group">
  <label class="col-md-2 control-label">Arquivo</label>
  <div class="col-md-10">
    <input name="Arquivo" type="file" class="btn btn-default" id="Arquivo">
    <p class="help-block"> Extensão permitida <strong>PDF</strong>. </p>
  </div>
</div>

Attempt to validate:

if (!isset($_FILES)):
    $retorno = array('codigo' => 0, 'mensagem' => ' Informe o arquivo para Upload');
    echo json_encode($retorno);
    exit();
endif;

The script is running even without informing the file.

    
asked by anonymous 19.12.2016 / 12:38

2 answers

1

You can do this:

if ($_FILES['Arquivo']['error'] == 4):
    $retorno = array('codigo' => 0, 'mensagem' => ' Informe o arquivo para Upload');
    echo json_encode($retorno);
    exit();
endif;

The problem was in verification, because isset($_FILES) is always true, $_FILES always exists, even if it is empty. What I did above was to check if inside that array there is Arquivo defined in the form.

More about upload errors

I also advise you to do a client side scan, with required :

<input name="Arquivo" type="file" class="btn btn-default" id="Arquivo" required>
    
19.12.2016 / 12:39
1

You can check the size of the file. If it is equal to 0 it is not uploaded.

if ($_FILES['Arquivo']['size'] == 0)
    
19.12.2016 / 12:40