What are the potential security concerns when using symlinks to access files outside the webroot in PHP?

Potential security concerns when using symlinks to access files outside the webroot in PHP include the risk of exposing sensitive information or allowing unauthorized access to files on the server. To mitigate this risk, it is important to properly validate and sanitize user input when constructing file paths and limit access to only necessary directories.

// Example of validating and sanitizing user input when using symlinks
$userInput = $_GET['file'];
$basePath = '/path/to/webroot/';
$filePath = realpath($basePath . $userInput);

if (strpos($filePath, $basePath) === 0) {
    // File is within the webroot, safe to access
    // Proceed with accessing the file
} else {
    // File is outside the webroot, do not allow access
    // Handle error or redirect to a safe location
}