0

The images are uploading normally, but I want an "else if" to check if there is any file selected. This is working:

    <?php
session_start();
    include('includes/conexao.php');
    $fileinfo=PATHINFO($_FILES["image"]["name"]);
    $newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
    move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
    $location="images/perfis/" . $newFilename;

$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");

if( mysqli_num_rows($todas_fotos) > 0) {
        //$path=$location;
        //if(unlink($path)) echo "Deleted file ";
        mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
}
else if( mysqli_num_rows($todas_fotos) == 0)
{
        mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
}
else {

};
    header('location:perfil.php');
?>

It inserts if there isn't an image, but if there is, it updates. But when I add:

else if (empty($_FILES['image']['name']))
{
header('location:perfil.php');
}

It returns me undefined index: extension on line 5. How to go?

4
  • first of all check print_r($_FILES) you will get the solution. Commented Sep 19, 2018 at 15:14
  • sorry, kinda newbie, how to do that? Commented Sep 19, 2018 at 15:35
  • write this after session start Commented Sep 19, 2018 at 15:36
  • there you go: "Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ) Notice: Undefined index: extension on line 6", i don't even... Commented Sep 19, 2018 at 15:40

1 Answer 1

1

Rewrite the code as follows. Here we are checking whether the file is not available at the beginning of the code and redirect if no file is found.

<?php
session_start();
if (empty($_FILES['image']['name']))
{
  header('location:perfil.php?error=1');
  return;
}
include('includes/conexao.php');
$fileinfo=PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
$location="images/perfis/" . $newFilename;

$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE 
img_usu_codigo = '" . $_SESSION['codigo'] . "'");

if( mysqli_num_rows($todas_fotos) > 0) {
    //$path=$location;
    //if(unlink($path)) echo "Deleted file ";
    mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
 }else {
    mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
 }

and in the perfil.php you should put

window.onload = function(){ var url = new URL(window.location.href); var error = url.searchParams.get("error"); if(error==1) alert("No file uploaded");

}

Sign up to request clarification or add additional context in comments.

10 Comments

This did the trick! Just one more question, where can I put a little script to show an alert that no files were selected?
this is perfect
i have tried using $message = "Você não selecionou uma imagem!"; echo "<script type='text/javascript'>alert('$message');</script>"; after the header, but didn't worked at all
@LuannSousa I should provide it as a separate code segment. I don't know whether we can submit it as another answer. The code perfil.php should be modified here
Edited please check
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.