Thursday, October 24, 2013

How to make a jpg image downloadale like a hyper linked file?

If you use the standard html link code for download JPG image file browser will open JPG file on browser. You can use php file to make JPG image file downloadable.

 
<a href="download-location/images/imagefile.jpg">download this picture</a> 
This will not work

You can use following code on php file name like imagedownloader.php

<?php 

// Force download of image file specified in URL query string and which 

// is in the same directory as this script: 

if(!empty($_GET['file'])) 

{ 

$filename = basename($_GET['img']); // don't accept other directories 

$size = @getimagesize($filename); 

$fp = @fopen($filename, "rb"); 

if ($size && $fp) 

{ 

header("Content-type: {$size['mime']}"); 

header("Content-Length: " . filesize($filename)); 

header("Content-Disposition: attachment; filename=$filename"); 

header('Content-Transfer-Encoding: binary'); 

header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 

fpassthru($fp); 

exit; 

} 

} 

header("HTTP/1.0 404 Not Found"); 

?>


Use link HTML tag as following. Replace imagefile.jpg the file as you wish to download

<img src="/download-location/images/imagedownloader.php?file=imagefile.jpg" alt="DownloadableImageLink">

0 comments:

Post a Comment