Is it a common practice to use readfile() in PHP to display images from restricted folders?

It is not a common practice to use readfile() in PHP to display images from restricted folders, as it can pose a security risk by exposing sensitive files. A better approach would be to use a combination of PHP code to read the image file and output it to the browser, while ensuring that access to restricted folders is properly controlled.

<?php
// Check if user has permission to access the image file
if (/* check user permissions here */) {
    $imagePath = '/path/to/restricted/image.jpg';
    
    // Output the image to the browser
    header('Content-Type: image/jpeg');
    readfile($imagePath);
} else {
    echo 'You do not have permission to access this image.';
}
?>