How can PHP be used to access and display images from directories that are not accessible to the web server?

To access and display images from directories that are not accessible to the web server, you can use PHP to read the images from the directory and then output them to the browser using appropriate headers. This can be achieved by using PHP's file system functions to read the images and then outputting them as binary data with the correct content type.

<?php
// Directory containing the images
$imageDir = '/path/to/images/';

// Get the list of images in the directory
$images = glob($imageDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// Output the images to the browser
foreach ($images as $image) {
    $imageData = file_get_contents($image);
    $imageType = mime_content_type($image);

    header('Content-Type: ' . $imageType);
    echo $imageData;
}
?>