Upload PHP does not work

Asked

Viewed 795 times

0

Good afternoon, I’m trying to make 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 doesn’t work. Echo’s $FILES["arquivo"]["error"] returns 0 as if you had successfully uploaded, but I go in the folder and there is nothing there. What is missing to work?

Thank you!

  • happens the "MOVEUU<br>"?

  • Does not happen. It shows the "Did not move" and the number 0 on the bottom line.

  • The folder it creates with all permissions OK, but without any file inside. As I can see this log?

  • 1

    One more remark: whenever you do multiple paths, note the 3rd parameter, the recursive in mkdir("D:/wamp64/www/revi/files/posts/".$post , 0777); - and note that the 2nd parameter is ignored in Windows. And check the value of $posts to see if the folder is right.

  • I made some changes here and is giving the following error: move_uploaded_file(): The Second argument to copy() Function cannot be a directory

  • 2

    You missed the variable inside (move_uploaded_file($arquivo['tmp_name'][$k], $diretorio), the correct one would be , $destino);, see the answer.

Show 1 more comment

1 answer

3


Has two mistakes:

  1. You put the $index variable instead of $destination here move_uploaded_file($arquivo['tmp_name'][$k], $diretorio)
  2. is_dir should be checked only in the mkdir part
  3. Extra: I don’t see the need to use 0777 (on Windows it makes no difference, but if on production it is a Unix-like server this could be a problem)
  4. Extra: there’s no point in rewriting the whole mkdir, take advantage of the variable you have already used

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

To fix 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"];
            }
        }
    }
  • It worked perfectly William. Thanks for the tips, so I can perfect my future codes.

Browser other questions tagged

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