File is being sent out of the folder defined with the move_uploaded_file

1
Hello, I'm doing a system where I previously create a folder, and later I upload the file (later I'll treat it if it's an image or video), the problem is that the move_uploaded_file method is ignoring the variables after the first slash and uploading in the first folder, not the user-defined folder:

$nome_pasta = $_POST['pasta']; //É o nome da pasta que o usuário criou na tela anterior do meu sistema.

if (isset($_POST['pasta'])){
    try{
        mkdir('./public/'. $nome_pasta) or die("erro ao criar diretório"); // Cria uma nova pasta dentro do diretório atual
        } catch (Exception $e){
            echo 'Exceção capturada: ',  $e->getMessage(), "\n";
        }   
    }
foreach ($_FILES["files"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["files"]["tmp_name"][$key];
        $name = $_FILES["files"]["name"][$key];
        move_uploaded_file($tmp_name, "public/$nome_pasta/$name" );
    }
}

I tried against bar but did not roll feeling too ... any ideas?

    
asked by anonymous 20.01.2017 / 14:54

1 answer

1

Use $_SERVER['DOCUMENT_ROOT'] to return the root directory under which the current script runs.

move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'] . "./public/$nome_pasta/$name" );

    
20.01.2017 / 15:28