Error while doing image update

Asked

Viewed 52 times

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 in php. because it works but 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{

    };
?>

Meu 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 to me are these.

inserir a descrição da imagem aqui

1 answer

1


The jQuery. serves to transform the data to the standard application/form-urlencoded, that is, it is not suitable for file uploads.

For file uploads, you can use Formdata, for example:

let formDados = new FormData(this);

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

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;
    });
});
  • Keeps making the same mistake.

  • @Edgnoizy if possible post your updated code on Pastebin.com and post the link here

  • https://pastebin.com/dZAguZMq

  • @Edgnoizy appears some error? I tested it here and it’s OK.

  • It says that the image does not exist, I tried to print the name of the variable that the img is and does not return me anything

  • @Edgnoizy I edited my answer.

  • I got what I was trying to do, but another little problem came up. I’m setting a folder for the image to go to it but the image is not going and when it will not appear the name of the image in the bank.

Show 2 more comments

Browser other questions tagged

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