PHP - Upload file in different folders

3

I have to make a PHP script that sends a single upado file to multiple folders (listed in the root of the server dynamically). The problem is that the first upload is done without problems, then I can not send it to the other folders.

EXEMPLO:

$dir = $_SERVER["DOCUMENT_ROOT"];
$scandir = scandir($dir);

$pastas = array();
foreach ($scandir as $pasta) {
    if($pasta == "." || $pasta == ".." || $pasta == ".htaccess" || $pasta == "default.htmlx" || $pasta == "error_log") {

    } else {
        $pastas[] = $pasta;
    }
}

if(isset($_POST['upload'])) {
    foreach ($pastas as $key => $pasta) {
        $arquivo = $_FILES['file']['name'];
        $file_tmp =$_FILES['file']['tmp_name'];

        $desired_dir = $dir."/".$pasta."/sistema/engine/";
        $caminho = $desired_dir.$arquivo;       

        $upload = move_uploaded_file($file_tmp, $caminho);
        if(!$upload) {
            echo "Erro ao realizar upload em: ".$caminho;
            exit(0);      
        }
    }

    echo "Deu certo.";
    exit(0);
}

Does anyone know where I might be going wrong or if there is any upload restriction in foreach?

    
asked by anonymous 10.06.2016 / 21:17

1 answer

3

When you use move_uploaded_file() you get the file that was temporarily saved and move it to another place, if you use it again, it will move the file again, but always a single file. To create another file use the copy, however, use the move_upload_file () first to have the checks that it does. Example:

move_uploaded_file($arquivo_temp, $destino1);
copy($destino1, $destino2);

I hope it helps.

    
10.06.2016 / 22:04