Upload PHP does not work

0

Good afternoon, I'm trying to do the file upload function via PHP using the code below:

  if(isset($_FILES['arquivo']))
        {            
            $diretorio = "D:/wamp64/www/revi/arquivos/postagens/".$postagem; 
            if(!is_dir($diretorio))
            {
                mkdir("D:/wamp64/www/revi/arquivos/postagens/".$postagem , 0777);
                $arquivo = $_FILES['arquivo'];  
                for ($k = 0; $k < count($arquivo['name']); $k++)
                {       
                    $destino = $diretorio."/".$arquivo['name'][$k];                 
                    if (move_uploaded_file($arquivo['tmp_name'][$k], $diretorio)) 
                    {
                        echo "MOVEUUUUUU<br>"; 
                    }                                
                    else 
                    {
                        echo "não moveu! <br>";
                        echo $_FILES["arquivo"]["error"];
                    }
                }      
            }

And my form is like this

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="arquivos[]" multiple/>
<button type="submit">Upload</button>
 </form>

But it just does not work. The echo of $FILES["arquivo"]["error"] returns 0 as if it had successfully uploaded, but I go in the folder and there is nothing there. What is missing to work?

Thank you!

    
asked by anonymous 20.11.2016 / 20:51

1 answer

3

You have two errors:

  • You put the $ directory variable instead of $ destination here move_uploaded_file($arquivo['tmp_name'][$k], $diretorio)
  • is_dir should be checked only in the mkdir part
  • Extra : I do not see need to use 0777 (Windows makes no difference, but if production is a unix-like server this might be a problem)
  • Extra : You do not have to rewrite the entire path in mkdir , enjoy the variable you have already used
  •   

    Important note : $FILES["arquivo"]["error"] does not guarantee the upload, it only guarantees that it arrived at the server in the ./tmp folder of your server, move_uploaded_file does move from ./tmp to% desired folder.

    To fix it do this:

    if(isset($_FILES['arquivo']))
    {            
        $diretorio = "D:/wamp64/www/revi/arquivos/postagens/".$postagem;
    
        //Verifica se a pasta já existe ou tenta cria-la
        $checarPasta = is_dir($diretorio) ? true : mkdir($diretorio, 0777);
    
        //Se não existir exibe um erro
        if(!$checarPasta)
        {
            echo 'Não pode criar ou acessar a pasta: ', $diretorio;
        }
        else
        {
            $arquivo = $_FILES['arquivo'];  
    
            for ($k = 0; $k < count($arquivo['name']); $k++)
            {       
                $destino = $diretorio."/".$arquivo['name'][$k];
    
                if (move_uploaded_file($arquivo['tmp_name'][$k], $destino)) 
                {
                    echo "MOVEUUUUUU<br>"; 
                }                                
                else 
                {
                    echo "não moveu! <br>";
                    echo $_FILES["arquivo"]["error"];
                }
            }
        }
    
        
    20.11.2016 / 21:33