Renomear arquivo carregado (php)

Estou tentando renomear um arquivo que estou carregando.

Vou carregar um arquivo xml ou pdf e quero que ele esteja em uma pasta chamada "arquivos /orderid/ "e o nome do arquivo também deve serorderid.extensão

O arquivo é carregado corretamente e a ID da pasta criada com o nome correto, mas todas as maneiras pelas quais tentei renomeá-lo falham.

Abaixo está o meu código.

// include database connection
 include 'config/database.php';

// get passed parameter value, in this case, the record ID
 $id=isset($_GET['orderid']) ? $_GET['orderid'] : die('FEJL: Ordren kunne ikke findes.');

// page header
 $page_title="Upload pdf og/eller xml fil";
 include_once "layout_head.php";

 echo "Ordrenummeret er ";
 echo $id;
 echo "<br>";

 if($_POST){

mkdir("files/$id");
$target_dir = "files/$id/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check if file already exists
if (file_exists($target_file)) {
    echo "Filen eksisterer allerede.";
    $uploadOk = 0;
}


// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Filen er for stor.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "xml" && $imageFileType != "pdf") {
    echo "Kun xml og pdf filer kan uploades.";
    $uploadOk = 0;
}


// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Filen blev ikke uploaded.";

// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Filen ". basename( $_FILES["fileToUpload"]["name"]). " er uploaded.";
    } else {
        echo "Der skete en fejl ved upload, prøv igen.";
    }
}
}
?>

 <!DOCTYPE html>
 <html>
 <body>

 <form action="upload_files.php?orderid=<?php echo htmlspecialchars($id); ?>" method="post" enctype="multipart/form-data">
Vælg xml eller pdf fil:
<input type="file" name="fileToUpload" id="<?php echo htmlspecialchars($id); ?>">
<input type="submit" value="Upload fil" name="submit">
 </form>

 </body>
 </html>

questionAnswers(0)

yourAnswerToTheQuestion