Block uploading images. gif renamed with . jpg or . png in PHP

Asked

Viewed 254 times

3

I have an uploading system where the user can upload only images with extension . jpg or .png. It follows part of the code:

$imagemNomeAntes = $_FILES["the_image"]["name"];
$imagemNomeAntesExt = basename($imagemNomeAntes);
$imagemNomeExt = pathinfo($imagemNomeAntesExt, PATHINFO_EXTENSION);
$permitidasExt = array("jpg", "jpeg", "png");

if(!in_array($imagemNomeExt, $permitidasExt)) {
    echo "Somente imagens com extensão jpg, jpeg ou png são aceitas!";
} else {
    $imagemNomeTmp = $_FILES["the_image"]["tmp_name"];
    $imagemNomeDir = "../photos";

    $imagemNomeEnd = md5($imagemNomeTmp).$imagemNomeExt;
    move_uploaded_file($imagemNomeTmp, ($imagemNomeDir.$imagemNomeEnd));

    imagedestroy($imagemNomeTmp);
}

The system works perfectly, however, if I go in an image with extension . gif and rename it to . jpg or . png upload happens and the image gets animated. How can I prevent this?

1 answer

4


Test as follows (present in PHP documentation)

<?php
if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
    echo 'Imagem não é um gif';
}
?>

Link to documentation: Documentation

  • Perfect! I hadn’t found this function, thank you very much!

Browser other questions tagged

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