File is not being saved to the folder specified with php

Asked

Viewed 43 times

0

I am trying to upload an ajax and php image, the name generated in php is saved as normal in the database but the file itself is not saved/moved to the "upload" folder within my project. Does not display any kind of error message.

If I give a var_dump($_FILES['file']['tmp_name'], $diretorio.$novo_nome); for example it prints

string(14) "/tmp/phpYoKhfM" string(43) "upload/47ebbe8e65ea9963b7480838fb34f1d6.jpg"

When checking the folder that locates in the project root there is nothing inside. I need some guidance on this.

<form method="POST" enctype="multipart/form-data" id="formulario">
   <input name="cpf" type="text" placeholder="CPF" id="cpf" readonly>
   <input type="password" name="senha" id="senha" placeholder="Nova senha">
   <input type="file" name="file" id="file">
   <input type="hidden" name="id">

   <input type="submit" value="Atualizar" id="nova-senha">
</form>

File that inserts the fields into the database and moves the image to the upload folder

try{
  //Pega a extensão do arquivo
  $extensao = strtolower(substr($_FILES['file']['name'], -4));

  //Gera o nome do arquivo
  $novo_nome= md5(time()) . $extensao;
  $diretorio = "upload/";

  move_uploaded_file($_FILES['file']['tmp_name'], $diretorio.$novo_nome);

  //Instancia o método para enviar o alguns valores
  $trocarSenha = new EsqueciSenha();
  $teste = $trocarSenha->alterarSenha($_POST['senha'], $_POST['id'], $novo_nome);
  echo json_encode($teste);
}
} catch (Exception $e) {
   echo "Erro";
}
  • Have you tried using the absolute path? $diretorio = __DIR__ . "/upload/";

  • I tried here and even then the folder remains empty, I tried even by way of doubt to create several folders in various places, but without success. I don’t know if it will make a difference but I use debian linux. Need some permission in the upload folder? @gmsantos

  • move_uploaded_file returns false? Enables warnings to get a better idea of what is happening.

  • You said something important the warnings, were having permission problems.

  • If someone has the same problem in linux system, and I migrated recently, I solved with ' chmod -R 777 your folder ', I used -R because I didn’t know where the file would fall.

No answers

Browser other questions tagged

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