Jump to content

Download Instead Of Opening


cclloyd9785

Recommended Posts

You have to make the server server them with a content disposition header. This would require a .htaccess file. Put the htaccess file in a folder called "downloads" or something, and put all your downloadable files in there.The .htaccess file

<FilesMatch "\.(html|jpg|png|gif)$">  header set Content-Disposition attachment</FilesMatch>

Add extensions to the <FilesMatch> expression separated by bars\.(ext1|ext2|ext3|ext4)$

Link to comment
Share on other sites

If you do it with PHP, you'll have to always link to the same PHP file and tell it which file to server. <a href="download.php?file=file.jpg">File</a> download.php:

<?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: application/octet-stream');header('Content-Disposition: attachment');// Output the file dataecho file_get_contents('downloads/' . $file);?>

Edited by Ingolme
Syntax error
Link to comment
Share on other sites

Put the htaccess file in a folder called "downloads" or something, and put all your downloadable files in there.The .htaccess file
<FilesMatch "\.(html|jpg|png|gif)$">  header set Content-Disposition attachment</FilesMatch>

Is the whitespace between the two lines necessary? Also, I had no idea that .htaccess file rules were applied to the directory they reside in. If I were to put a bad line of code in the root directory of my site, that'd mess it up, but if I put a bad line of code in a .htaccess file in a sub-directory, it'd be fine? Edited by Coaxsist
Link to comment
Share on other sites

Sorry, I'm hijacking this a bit... I'm managing a site with a lot of PDF files. Ideally, I'd like a single directory with meeting minute pdfs with two types of links that point to files in that directory. A "view" link, which would point directly to the file and open in the browser, and a "download" link, which would open a download dialog box. If there was the above .htaccess file in that dir. would the "view" links open a download dialog instead of the browser just opening the pdf? Would the php solution allow both links?

Link to comment
Share on other sites

Well, why don't you just make the "view" link point directly to the PDF file, and the "download" link point to a download.php script like Ingolme presents? Then you don't need a server configuration file.

Link to comment
Share on other sites

You just have to change 'downloads/' for the path to your downloads directory relative to the PHP file in the following lines:

if(!file_exists('downloads/' . $file) {......echo file_get_contents('downloads/' . $file);

Link to comment
Share on other sites

If the download.php file is in the exact same directory as the files, then you don't need to put the folder. Put download.php outside the folder. This way people can't use the file to download itself and see its source code.

Link to comment
Share on other sites

  • 2 years later...

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);?>
Edited by Vertex21
Link to comment
Share on other sites

Changing the content-type won't fix or break the script. If you're only going to use it to download PNG images, you can use image/png, if the script is going to be used to download multiple different types of file it's easier to use application/octet-stream rather than try to determine what MIME type to relay to the browsers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...