How can temporary images be used in PHP to prevent users from viewing complete file paths when viewing images on a webpage?

To prevent users from viewing complete file paths when viewing images on a webpage, you can use temporary images in PHP. This involves storing the images in a temporary directory on the server and serving them to users through a PHP script. By doing this, users will not be able to access the actual file paths of the images on the server.

<?php
// Path to the directory containing the images
$imageDirectory = '/path/to/images/';

// Generate a random filename for the temporary image
$tempImage = tempnam(sys_get_temp_dir(), 'img_');

// Copy the original image to the temporary file
copy($imageDirectory . 'image.jpg', $tempImage);

// Set the appropriate headers to display the image
header('Content-Type: image/jpeg');
readfile($tempImage);

// Delete the temporary image after it has been served
unlink($tempImage);
?>