This topic is locked

File Preview instead of Download

5/16/2025 3:36:07 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

There was a previous post and I am doing what it said, but my browser always downloads the image rather than preview it.

img alt

Any ideas?

Alan

C
Chris Whitehead 5/21/2025

Without knowing what the previous post says, you could try sending the image headers in a blank page, just supply the image path or record.

// Specify the path to the image
$imagePath = 'path/to/image.jpg';// from the database or in a $_GET

// Check if the file exists
if (file_exists($imagePath)) {
// Get the image's MIME type
$mimeType = mime_content_type($imagePath);

// Set the appropriate headers
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($imagePath));

// Output the image content
readfile($imagePath);
exit;
} else {
// Handle the error if the file doesn't exist
http_response_code(404);
echo 'Image not found.';
}