Are there security concerns to consider when allowing users to input directory paths in PHP forms?

When allowing users to input directory paths in PHP forms, there are security concerns to consider such as directory traversal attacks where users can potentially access sensitive files on the server. To mitigate this risk, it is important to validate and sanitize user input to ensure that only allowed characters are used in the directory path. One way to do this is by using PHP's `realpath()` function to normalize the directory path and check if it falls within an allowed directory.

$user_input = $_POST['directory_path'];

$allowed_directory = '/path/to/allowed/directory/';

$normalized_path = realpath($user_input);

if (strpos($normalized_path, $allowed_directory) !== 0) {
    // Invalid directory path, handle error
} else {
    // Valid directory path, proceed with processing
}