Hi, all; My first post here.

I want to offer my users the ability to click on an icon, and download the hi-res version of the related image. Since I already have the album set up to show a screen-resolution image, there is no need to show it at print resolution hence I want to avoid going through the right click/save as routine.

I am using php on the server side, so I have plenty of flexibility to modify headers, etc.

Trouble is, I can't think of how to do it, so any help is greatly appreciated.

Peter

Dani AI

Generated

Briefly: a server-side download handler is the right way to give a one-click “save hi‑res” experience. ’s snippet shows the core idea, but production use needs a few extra steps: never trust a raw filename from the URL, detect the correct MIME type, send a proper Content‑Disposition (with a safe filename), and stream or offload large files so the webserver—not PHP—does the heavy lifting.

Key practices to add

  • Don’t accept arbitrary paths. map an ID to a filepath or verify realpath() is inside your images directory to avoid path traversal.
  • Detect MIME with finfo_file() (fallback to application/octet-stream) rather than hardcoding image/jpeg.
  • Send Content-Length and a Content-Disposition attachment with a safe ASCII fallback and a UTF‑8 filename* fallback when needed.
  • For big files use X-Sendfile / X-Accel-Redirect or stream in chunks; always clear output buffers before streaming.

Example of a minimal, safer PHP handler (map IDs to files, detect MIME, stream):

<?php
// safe-download.php?id=123
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$map = [ 1 => '/var/www/site/images/hires/photo1.jpg', 2 => '/var/www/site/images/hires/photo2.jpg' ];
if (!isset($map[$id]) || !is_file($map[$id])) { header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); exit; }
$file = $map[$id]; $name = basename($file);
$finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file) ?: 'application/octet-stream'; finfo_close($finfo);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$name.'"; filename*=UTF-8\'\''.rawurlencode($name));
header('Content-Length: '.filesize($file));
while (ob_get_level()) ob_end_clean();
$fp = fopen($file, 'rb'); if ($fp) fpassthru($fp);
exit;
?>

Additional notes: for public files the HTML5 download attribute on the anchor tag can be a zero‑effort option, but it’s subject to same‑origin and browser quirks. If images are private, enforce session/auth before sending headers or use time‑limited signed URLs. Test in multiple browsers (desktop and mobile) and consider using server sendfile features for scale.

Recommended Answers

All 3 Replies

saveimage.php

<?php if(!$file) return false;
header("Content-disposition: attachment; filename=$file");
header('Content-type: image/jpeg;');
readfile("$file");
?>

image link

<a href='saveimage.php?file=hires/image.jpg'><img src='lowres/image.jpg' alt='click to save hires image'></a>

cool.
Thanks for that.
Peter

Generic file download
savefile.php

<?php 
if(!$file) return false;
if(!type) return false;
header("content-disposition: attachment; filename=$file");
header("content-type: $type");
readfile("$file");?>

download link

<a href='savefilee.php?file=pathtofile&type=type'>save this file</a>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.