Are there any potential security risks associated with using the mkdir function in PHP to create folders?

When using the mkdir function in PHP to create folders, there is a potential security risk if user input is directly used in the function without proper validation. This can lead to directory traversal attacks where an attacker can create folders in unintended locations on the server. To mitigate this risk, always sanitize and validate user input before passing it to the mkdir function.

$folderName = filter_var($_POST['folder_name'], FILTER_SANITIZE_STRING);
$basePath = '/path/to/your/directory/';
$fullPath = $basePath . $folderName;

if (!file_exists($fullPath)) {
    mkdir($fullPath, 0777, true);
    echo 'Folder created successfully.';
} else {
    echo 'Folder already exists.';
}