What potential issues could arise when trying to access images using readfile() in PHP?

One potential issue when using readfile() in PHP to access images is that it may not work correctly with certain file types or sizes, leading to errors or incomplete image rendering. To solve this issue, you can use the header() function to specify the content type of the file being read before outputting it with readfile(). This ensures that the browser interprets the file correctly.

<?php
$file = 'path/to/image.jpg';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: image/jpeg');
    header('Content-Disposition: inline; filename="' . basename($file) . '"');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
} else {
    echo 'Image not found.';
}
?>