How can an image saved in PHP be accessed via URL for retrieval?

When saving an image in PHP, you can store the image path in a database along with a unique identifier. To access the image via URL for retrieval, you can create a PHP script that retrieves the image path from the database based on the unique identifier provided in the URL. The script can then output the image with the appropriate headers to display it in the browser.

<?php
// Connect to database and retrieve image path based on unique identifier
$imageId = $_GET['id']; // Assuming the unique identifier is passed in the URL
// Query database to get image path based on $imageId

// Output image with appropriate headers
header('Content-Type: image/jpeg'); // Adjust content type based on image type
readfile($imagePath); // Output image file