I am doing an image insertion with jquery ajax and php but the move_uploaded_file is not working it does not send anything

Asked

Viewed 74 times

-1

HTML

 <form id="formImage" enctype="multipart/form-data">
        <div class="corpo">
          <div class="foto">
            <h5 class="white-text title">Escolha sua foto de perfil</h5>
            <br>
            <div class="imagem">
                <label for="inputImagem"><img 
  src="https://cdt.org/files/2015/10/2015-10-06-FB-person.png" alt id="img_perf" 
 class="circle z-depth-2"></label>
                <input type="file" id="inputImagem" name="foto">
            </div>
            <br>
    </form>

            <br>
            <a class="btn cor_tema btn_proximo" id="prox1"><i class="material-icons">arrow_forward</i></a>
          </div>

JS

$("#prox1").click(function(){

  var fd = new FormData();
  var files = $('#inputImagem')[0].files [0];
  fd.append ('foto', files);

  $.ajax({
    url:"http://localhost/servidor/insereImg.php",
    dataType:'json',
    type:'POST',
    data:fd,
    contentType: false,
    processData: false,
    success:function(r){
      if(r.Resp==1){
          localStorage.setItem("Foto","http://localhost/servidor/img/" + r.Foto);
        }
      },
    error:function(e){
        console.log(e);
      }

  })
});

PHP

<?php

header('Content-type: application/json');
header('Access-Control-Allow-Origin:*');

if ($_FILES['foto']) {
          date_default_timezone_set('America/Sao_Paulo');
          $data = date('Y-m-d H:i:s');
          $extensao = substr($_FILES['foto']['name'],-4);
          $novoNome = md5($data).$extensao;
          $diretorio = '/http://localhost/servidor/img/';
          move_uploaded_file($_FILES['foto']['tmp_name'], $diretorio.$novoNome);
          $img = $novoNome;
        }

  $resposta = array('Resp' => '1',
                    'Foto' => $img
  );





ob_clean();
echo json_encode($resposta);
 ?>

1 answer

0


In the code PHP, there is an error in the variable value $diretorio.

$diretorio = '/http://localhost/servidor/img/';

It should have the complete path from where the upload files will be stored. Example:

$diretorio = '/var/www/html/upload_de_imagens/';

UPDATE: On windows servers, you can use:

$diretorio = 'c:\\xampp\\htdocs\\servidor\\img\\';

PHP code corrected:

<?php

header('Content-type: application/json');
header('Access-Control-Allow-Origin:*');

if ($_FILES['foto']) {
          var_dump($_FILES['foto']['name']); // testa se foi postado

          date_default_timezone_set('America/Sao_Paulo');
          $data = date('Y-m-d H:i:s');
          $extensao = substr($_FILES['foto']['name'],-4);
          $novoNome = md5($data).$extensao;

          // $diretorio = '/http://localhost/servidor/img/'; // incorreto
          $diretorio = 'c:\\xampp\\htdocs\\servidor\\img\\'; // correto

          move_uploaded_file($_FILES['foto']['tmp_name'], $diretorio.$novoNome);
          $img = $novoNome;
        }

  $resposta = array('Resp' => '1',
                    'Foto' => $img
  );





ob_clean();
echo json_encode($resposta);
 ?>

I hope I’ve helped.

  • still not understood how so I would have to put the walk starting from the form page ???

  • Is your server windows or linux? What folder are the PHP codes in? Example: /var/www/html/ or C: wamp www\

  • c: xampp htdocs img server\

  • I did an update on the answer, test now tell me if it worked.

  • Cara vc helped me a lot thanks, but now I’d like to know how I make it work on a web server. o 000.Webhost

  • @Jottavideos, if it worked, I ask you to choose my answer and mark it with +1. For the new question, create a new question and I’ll help you there (just send me the link).

Show 1 more comment

Browser other questions tagged

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