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

Asked

Viewed 55 times

1

Hello, I am making a system where I previously create a folder, and later I upload the file (later I will treat whether it is image or video), the problem is that the method move_uploaded_file is ignoring variables after the first bar and uploading to the first folder, not to 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" );
    }
}

Print do problema

I tried against bar but there was no feeling either... any idea?

  • If you really try: move_uploaded_file($tmp_name, "public/Teste/$name" ); he does the supposed?

  • The man I mistook for the answer...

1 answer

1

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

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

Browser other questions tagged

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