FORCING BROWSERS TO DOWNLOAD IMAGES (OR OTHER FILES) - Nov 27th, 2011


There are times that you'd like a visitor to be able to download images (and other files, like PDFs) instead of just
displaying them in line.

Here's a suggestion that originally came from Dave Edis at Interactive Tools.

He suggested using an .htaccess file to do the job.

Here's my version which presents a download dialog box when clicked on for all jpg,psd and tif or tiff files. This only
affects files when you're linking to them, as in the code:



<?php foreach ($your_tableRecord['your_upload_field'] as $upload): ?>

<a href="<?php echo $upload['urlPath'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo
$upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /></a><br />

<?PHP endforeach; ?>


Here's the .htaccess code



<FilesMatch "\.(jpg|psd|tif|tiff)$">
ForceType application/octet-stream
</FilesMatch>


According to Shi Chuan on his blog at
http://www.blog.highub.com/apache/http-server/force-files-like-pdf-download-using-htaccess/ "If you view the files in a
web page, they will display, but if linked directly to them they will show you the save box. (Files don’t seem to
download if they are saved in cache Ctrl+F5 to fix that)."



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service