move_uploaded_file does not work properly

Asked

Viewed 494 times

0

I’ve tried every way move_uploaded_file() move the desired file.

I set up a simple image upload form, but it doesn’t move at all.

POST:

$foto = $_FILES["txtFoto"];

// Caminho de onde ficará a imagem 
$caminho_imagem = "images/"; 

// Faz o upload da imagem para seu respectivo caminho 
if (move_uploaded_file($foto['name'], $caminho_imagem)) {
    $uploaddir = 'images/'.$foto['name'];
    if(isset($_GET['d'])){
        $id = $_GET['d'];
        produtos::altera($id,$nome,$descricao,$uploaddir);
        $dados = produtos::carrega($id);
    }else{
        produtos::cadastra($nome,$descricao,$uploaddir);
    }
?>
    <div class="alert alert-success" id="respyes">Produto alterado/cadastrado com sucesso!</div>
<?php   
} else {
    echo "nome do arquivo '". $foto['name'] . "'.";
}

This is a part of my code and I’ve looked at folder permission, syntax and all the rest, but it doesn’t work :(

  • The first thing that needs to be noticed is that the use of move_uploaded_file is wrong, the way the colleague @deoliveiralucas posted is correct. According to which image folder is created?(remembering that they will be in the same script folder) it has the correct permissions?

2 answers

1

Have you tried using the tmp_name? thus:

if (move_uploaded_file($foto['tmp_name'], $caminho_imagem . $foto['name'])) {
  • Yes. And it still didn’t work x.x

  • What is this code you posted returning? which returns the var_dump($foto)?

  • His var_dump returns the correct image data I’m using for testing: array(5) { ["name"]=> string(17) "touch60.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(24) "C:\xampp\tmp\php8492.tmp" ["error"]=> int(0) ["size"]=> int(3756) }

  • try like this: if (move_uploaded_file($foto['name'], 'images/'.$foto['name'])) {

  • I had tried before and tried again now and it didn’t work :(

  • That code is falling into condition if or else?

Show 1 more comment

0

Try the following amendment:

move_uploaded_file($_FILES['txtFoto']['tmp_name'], $caminho_imagem.$_FILES['txtFoto']['name']);

Browser other questions tagged

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