Take the name of an image by Php or JS

Asked

Viewed 1,190 times

1

Well, I have a form that has a input of type ='file'. I’ve seen on several sites and examples of how to do and all that I test do not work. the code can not share more is more or less like this.

I have a form with input file and an input button on the button the JS calls a function that takes the values of form and makes a array(JSON), then do Ajax with PHP for insertion in the database.

Only that the array when this in PHO does not come the name of the image. Blz so I did direct in PHP like this: $img = $_FILES["arquivo"]["name"]; and it also returns the variable empty. All is correct form with the enctype, more anyway does not work.

Html:

<form action="?pagina=cadastroEmpresas" method="POST" id="formEmpresa" name="formEmpresa" enctype="multipart/form-data">
<input value="Salvar" id="btnSalvarForm" style="padding: 5px; font-size: 14px; margin-left: -13px;" type="button">
<label for="arquivo">Logo:</label> <input name="arquivo" id="arquivo" value="" type="file" />

JS:

$( "#btnSalvarForm" ).on("click", function() {
    inserirEmpresa();
});
function inserirEmpresa(){
    campos = pegarCampos( "formEmpresa" );
    $.ajax({
        type : "POST",
        data : {dados:JSON.stringify(campos)},
        url : "php/buscaEmpresasBD.php?escolha=7",
        success : function(resposta){
            alert(resposta);
            $("#divCamposForm").css("display", "none");
        }//fim success      
    });//fim ajax
}

PHP:

function inserirEmpresa(){
        global $cnx;
        //echo $_POST['dados'];
        $dados = json_decode($_POST['dados'], TRUE);
        //echo $dados["txtNomeFantasiaEmpresa"];
        $target_dir = "galeria/";
        //$target_file = $target_dir . basename($_FILES["arquivo"]["name"]);
        $target_file = $target_dir . basename($_FILES["arquivo"]["name"]);
        echo "$target_file";
}
  • So without giving more data or showing the code it is difficult to help.

  • If everything is correct the problem must be something else, put the code you have.

  • The input[type=file] submits only the byte array that represents the local physical file. If you want to submit the file name you need to assign by JavaScript to another input the name of the selected file displayed in input[typr=file].

1 answer

0


If you are using ajax, file does not upload by ajax. But there are some methods you do that work.

A few questions about that:

https://stackoverflow.com/questions/19447435/ajax-upload-image
Upload a file with AJAX

But in case you want to catch the name (not the file) - the file by JS (jQuery) does so:

$('.file').on('change', function(){

    // Pega o Nome do Arquivo
    var fileName = $('input[type="file"]')[0].files[0].name;

    // Coloca num campo Hidden com ID #file-name
    $('#file-name').val(fileName);

});
  • then even using this your code above it says that file is undefined!

  • But you edited the code according to your ? I put a class .file in the onchange. Your input file is with a class called file ?

  • 2

    Ufaa! Now yes! Thanks Diego !! Save my service kkk

Browser other questions tagged

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