What potential issues can arise when using mkdir with variables in PHP?

When using mkdir with variables in PHP, the issue that can arise is that the directory path might not be properly sanitized, leading to potential security vulnerabilities such as directory traversal attacks. To solve this issue, it is important to properly sanitize and validate the input before using it in the mkdir function.

// Example of properly sanitizing and validating input before using mkdir
$directory = '/path/to/directory/' . basename($_POST['directory_name']);
if (preg_match('/^[a-zA-Z0-9_-]+$/', $_POST['directory_name'])) {
    mkdir($directory);
} else {
    echo 'Invalid directory name';
}