$_FILES Undefined index error how to resolve

Asked

Viewed 1,524 times

1

When uploading an image I get the error:

Notice: Undefined index: imagem in C: Apache24 htdocs change_photo.php on line 9

Notice: Undefined index: imagem in C: Apache24 htdocs change_photo.php on line 10

Calling for:

<script type="text/javascript">
            $(document).ready(function(){
                
                $('#imagem').live('change',function(){
                    var url1 = "change_photo.php";
                    var email1 = "<?php echo $_SESSION['email']?>";
                    var fileVal=document.getElementById("imagem");
                    var fakeFile = fileVal.value;
                    $.post(url1,{postemail1:email1,foto:fakeFile},
                        function(result) {
                        $("#visualizar").html(result); // Só pra verificar retorno
                    });
                
                });
            }) 
</script>

Form:

<form id="formulario" method="post" enctype="multipart/form-data" action="change_photo.php">
        Foto
        <input type="file" id="imagem" name="imagem" /> 
    </form>

Change_photo.php:

 include('conexao.php');
 $pasta = "fotos/";

 /* formatos de imagem permitidos */ 
 echo $_POST['foto'];

 $permitidos = array(".jpg",".jpeg",".gif",".png", ".bmp");

    $name = $_FILES['imagem']['name'];
    $tmp_name = $_FILES['imagem']['tmp_name'];

    $error = $_FILES['imagem']['error'];

Would anyone know where the problem is?

  • need to be uploaded using jquery? the error I believe you are making this $.post via jquery. Just try with php and html

1 answer

1


You are passing the value of the file to a "common" post in this ajax function. The input file has its own behavior, so it has the enctype="multipart/form-data" in the form. Just give a normal Ubmit that the value will be populated in the $_FILE and it should work.

  • I did what you said: I included a Submit and took the ajax part... and it worked ! Thank you very much for the help

Browser other questions tagged

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