Error updating image

0

I'm trying to update an image with HTML, Ajax and PHP.

HTML and Ajax are configured correctly, as far as I know. But I think the problem is being in php. because it works but it returns me error. my php.

<?php
    require_once('conexao.php');

    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $Trocar_foto_perfil = $_FILES['Trocar_img_perfil']['name'];
        echo 'Esse é o nome da minha imagem : '.$Trocar_foto_perfil;

        session_start();
        $On_muda_foto = $_SESSION['Foto_s'];
        echo 'Esse é o ID : '.$On_muda_foto;

        $_UP['pasta'] = 'images-profile/';
        $_UP['tamanho'] = 1024*1024*100;
        $_UP['extensoes'] = array('png','jpg','jpeg');
        $_UP['renomear'] = false;

        if($Trocar_foto_perfil == ""){
            echo "Cororreu algum erro com a imagem!";
        }

        if(array_search($extensao, $_UP['extensao']) === false){
            echo "Rapaz, não adianta tapear não. A img tem a extensão errada!";
        }

        if($_UP['tamanho'] < $_FILES['Trocar_img_perfil']['size']){
            echo "kkkkjj ae tiw a img é muito grande nois num tem host caro n poh!";
        }

        else{
            $Nome_img_perfil = $_FILES['Trocar_img_perfil']['name'];
            $Nome_img_perfil_final = strtolower(preg_replace("[^a-zA-Z0-9-]", "-",strtr(utf8_decode(trim($Nome_img_perfil)),utf8_decode(" "),"-")));

            if(move_uploaded_file($_FILES['Trocar_img_perfil']['tmp_name'], $_UP['pasta']. $Nome_img_perfil_final)){
                $SQL = mysqli_query($conex," UPDATE User_log SET Foto='$Nome_img_perfil_final' WHERE ID=$On_muda_foto");
                echo '<script>history.go(0);</script>';
            }

            else{
                echo "Ocorreu um erro!";
            }
        }
    }
    else{

    };
?>

My Ajax

<script>
        $(function(){
            $(".form6").submit(function(event){
                event.preventDefault();
                var formDados = $(".form6").serialize();

                $.ajax({
                    url:"/Arquivo/trocar_img.php",
                    type:"POST",
                    data:formDados,
                    cache:false,
                    processData:false,
                    success:function(data){
                        $("#ressult_log_succes_img_plus").html(data);
                    },
                    error:function(data){
                        $("#ressult_log_error_img_plus").html(data);
                    },
                    dataType:"html"
                });
                return false;
            });
        });
    </script>

The error that returns me is these.

    
asked by anonymous 01.03.2018 / 16:26

1 answer

1

The jQuery.serialize serves to transform the data to the default application/form-urlencoded , ie it is not for uploads of files.

For file uploads, you can use FormData , for example: / p>

let formDados = new FormData(this);

Also add contentType: false , this way you will force jQuery to send with the correct Content-Type .

Full example:

$(function(){
    $(".form6").submit(function(event){
        event.preventDefault();
        let formData = new FormData(this);

        let xhr = new XMLHttpRequest();
        xhr.open("POST", "index2.php", false);
        xhr.send(formData);

        $.ajax({
            url:"index2.php",
            type:"POST",
            data:formData,
            cache:false,
            processData:false,
            contentType: false,
            dataType:"html",
            success:function(data){
                $("#ressult_log_succes_img_plus").html(data);
            },
            error:function(data){
                $("#ressult_log_error_img_plus").html(data);
            }
        });
        return false;
    });
});
    
01.03.2018 / 16:44