How do I know $_FILES is empty?

Asked

Viewed 1,201 times

0

Hello, I tested some solutions of Stackoverflow itself but did not succeed, it "jumps" the IF and goes straight to the Else, even the field containing some information.

$tipo_servico = $_POST['tiposervico'];
$nome = $_POST['nome'];
$descricao = $_POST['descricao'];
$img = $_FILES['imagem'];

include "conexao.php";

if ($_FILES['imagem']['size'] > 0){  
    /*Separar o nome da imagem */
    $titulo_img = $img['name'];

    /*Separando o caminho da imagem temporariamente*/
    $tmp = $img['tmp_name'];

    /*Separar extensão */
    $formato = pathinfo($titulo_img, PATHINFO_EXTENSION);

    /*Renomeando a imagem*/
    $novo_nome = uniqid().".".$formato;

    if(($formato == "jpg" || $formato == "png")){

        $sql = "UPDATE servico_tb SET
        tipo_servico = '$tipo_servico'
        nome  = '$nome',
        descricao = '$descricao'            
        nome_foto = '$novo_nome'
        WHERE 
        id = '$id'
        ";      

        $editar_servico = $conexao -> prepare($sql);
        $editar_servico -> execute();
        echo"<script>alert('DASDFSDFGFG!');</script>";
    }else{
        //echo"<script>alert('Apenas arquivos JPG e PNG!');</script>";
        //echo("<script>location.href='admin.php';</script>");
    }
}else{
    echo "Erro";
}

Also some snippets of HTML code

<form style="max-width: 330px; padding: 15px; margin: 0 auto;" action="" method="POST" enctype="multipart/form-data">

            <label><strong>Foto atual do serviço</strong></label>
            <img src="imgservicos/<?php echo $conexao['nome_foto'];?>" width="250px" height="250px">
            <!-- --><br>
            <!-- --><br>
            <label><strong>*Nova foto do serviço</strong></label>
            <h5><strong>OBS:. Não é necessário atualizar a foto.</strong></h5>
            <input type="file" name="imagem" class="form-control"> 
            *Somente arquivos JPG e PNG. <br>
            <input class="btn btn-lg btn-primary btn-block"  type="submit" name="enviar" value="Editar informações">
        </form>
  • What is the return of the $format variable? From a var_dump and put the result

  • @Luizgustavocostaceolin I tried here, and even using the solutions below the var_dump returns something, as if ignoring the if.

2 answers

1


  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • But I didn’t just put the link put up as it can check.

  • You should explain how it works and use the link only for ref

0

An alternative is to use the function empty():

if (empty($_FILES['imagem']['size']) != false){  
    /*Separar o nome da imagem */
    $titulo_img = $img['name'];

    /*Separando o caminho da imagem temporariamente*/
    $tmp = $img['tmp_name'];

Returns FALSE var exists and is not empty and does not contain a zeroed value. Otherwise it will return TRUE.

Browser other questions tagged

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