Jump to content

Vertex21

Members
  • Posts

    1
  • Joined

  • Last visited

Vertex21's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Since this post has a high organic search rating in Google for "how to make a link download a photo instead of view it", I thought I'd contribute in case others come here and realize the php solution presented does not work. I actually wonder if the member who posted this solution actually tested the code prior to posting? Anyways, here's the block of code that Ingolme posted, except tweaked and updated so that it actually works. <!-- ASSUMING LINK TO IMAGE (OR FILE) IS AS FOLLOWS --><!-- EXAMPLE GIVEN IS MY REAL WORLD USE IN THE RESULTS OF A MYSQL QUERY --><?php<a href="/_images/download.php?file='.$row['img_path'].'" class="viewEntry">download photo</a>?>_____________________________________________________________________________________<?php// Make sure only files in this directory are accessible by removing slashes$file = str_replace('/','',$_GET['file']);$file = str_replace('','',$file);// If the file doesn't exist, show an error message and leaveif(!file_exists('downloads/' . $file)) { header("HTTP/1.0 404 Not Found"); echo 'The selected file doesn't exist'; exit;}// Tell the browser to download the fileheader('Content-type: image/png');header('Content-Disposition: attachment; filename=' . $file);// Output the file dataecho file_get_contents('downloads/' . $file);?>
×
×
  • Create New...