Adriano Machado
Adriano Machado

Reputation: 107

Wrong "if" logic PHP

I think I didn't understand the PHP logic yet. My "if's" in javascript works fine, but in PHP it's always ignored. I have this form (party of it) in insert_car.php:

<input type="file" name="imagem01" id="imagem01" title="Imagem 01" onchange="readURL01(this);" class="button" />

... (and 5 more inputs for images and others for texts)

this is the correspondent party in insert_db.php:

$gravar_imagem01 = $_FILES['imagem01'];

preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $gravar_imagem01["name"], $ext);

$nome_imagem01 = md5(uniqid(time())) . "." . $ext[1];
$caminho_imagem01 = "../../images/" . $nome_imagem01;

move_uploaded_file($gravar_imagem01["tmp_name"], $caminho_imagem01);

$sqlinsert = "INSERT INTO tb_carros (... imagem01,...) value (... '$nome_imagem01',...)";

It works fine, but know I want to put an default image if one of the inputs file is empty. So I did this in insert_db.php:

if (empty($imagem01))
{
    $gravar_imagem01 = "sem_imagem.jpg";
    $nome_imagem01 = $gravar_imagem01;
}
else
{
    $gravar_imagem01 = $_FILES['imagem01'];

    preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $gravar_imagem01["name"], $ext);

    $nome_imagem01 = md5(uniqid(time())) . "." . $ext[1];
    $caminho_imagem01 = "../../images/" . $nome_imagem01;

    move_uploaded_file($gravar_imagem01["tmp_name"], $caminho_imagem01);
}

The default image (sem_imagem.jpg) is been placed, but even if the input "imagem01" is not empty the default image appears. So, what is wrong in my code?

Ps.: If I switch the "if" and "else" condition the result is the reverse (the default image don't appears even with the input imagem01 empty). That's why I said that my "if" is been ignored.

Upvotes: 0

Views: 103

Answers (4)

pilsetnieks
pilsetnieks

Reputation: 10420

There is no variable named $imagem01 anywhere else in your code so it is always going to be undefined and empty($imagem01) will always return true. You may need to check for a different condition in your if statement.

Upvotes: 0

mohammad mohsenipur
mohammad mohsenipur

Reputation: 3149

try this i think always $imagem01 is empty you must get imagem01 data from form like $imagem01= $_FILES['imagem01']; then check for empty you can't use JavaScript variable directly it better in this case use var_dump($var); for see variable value

 $imagem01= $_FILES['imagem01'];
 if (empty($imagem01))
 {
   $gravar_imagem01 = "sem_imagem.jpg";
   $nome_imagem01 = $gravar_imagem01;
 }
  else
  {
    $gravar_imagem01 = $_FILES['imagem01'];
    preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $gravar_imagem01["name"], $ext);
    $nome_imagem01 = md5(uniqid(time())) . "." . $ext[1];
    $caminho_imagem01 = "../../images/" . $nome_imagem01;
    move_uploaded_file($gravar_imagem01["tmp_name"], $caminho_imagem01);
 }

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

The if isn't being "ignored"; you likely misunderstood the meaning of the function empty. Look it up in the documentation, and check that it does what you think it does.

In particular, it doesn't magically understand the concept of "files" — empty looks at variables. It is quite conceivable that $_FILES['imagem01'] still exists in some form even in the case in which you're perceiving the image file to be "empty". I suggest examining it in this case to find out which conditional to use.


Edit: It also helps if you use the correct variable name:

if (empty($imagem01))

becomes:

if (empty($_FILES['imagem01']))

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Try this with isset

change this

if (empty($imagem01))

to

$gravar_imagem01 = $_FILES['imagem01'];
if (isset($gravar_imagem01))

Upvotes: 1

Related Questions