cclloyd9785 Posted September 27, 2011 Share Posted September 27, 2011 Not sure what language this is, so I'll post it here, and it can be moved if needed. How would I make a link to items download instead of open in the browser. Things like images and html files. 1 Link to comment Share on other sites More sharing options...
Ingolme Posted September 28, 2011 Share Posted September 28, 2011 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 More sharing options...
cclloyd9785 Posted September 28, 2011 Author Share Posted September 28, 2011 That .htaccess content is not allowed by my host. Now that I think about it, is there maybe a PHP to make all file types of my choosing in a directory download instead of opening? Link to comment Share on other sites More sharing options...
Ingolme Posted September 28, 2011 Share Posted September 28, 2011 (edited) 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 October 9, 2011 by Ingolme Syntax error Link to comment Share on other sites More sharing options...
cclloyd9785 Posted September 28, 2011 Author Share Posted September 28, 2011 Tried it out, and got an error when trying to download something to test it out. Parse error: syntax error, unexpected '{' in /www/zxq.net/c/c/l/cclloyd/htdocs/download/download.php on line 6 Link to comment Share on other sites More sharing options...
Synook Posted September 29, 2011 Share Posted September 29, 2011 There's just a missing closing bracket on that line... Link to comment Share on other sites More sharing options...
knystrom18 Posted October 5, 2011 Share Posted October 5, 2011 (edited) 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 October 5, 2011 by Coaxsist Link to comment Share on other sites More sharing options...
knystrom18 Posted October 5, 2011 Share Posted October 5, 2011 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 More sharing options...
knystrom18 Posted October 6, 2011 Share Posted October 6, 2011 Bump! Does anyone know? My instinct is that with the .htaccess file present, the "view" links would open dialog boxes. Link to comment Share on other sites More sharing options...
Synook Posted October 7, 2011 Share Posted October 7, 2011 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 More sharing options...
knystrom18 Posted October 8, 2011 Share Posted October 8, 2011 That'll work... I feel dumb. Thanks Synook, Ingolme. Link to comment Share on other sites More sharing options...
cclloyd9785 Posted October 9, 2011 Author Share Posted October 9, 2011 I still get an error, with every file saying it does not exist. I've tried doing ?file=proof.html, ?file=http://cclloyd.zxq.net/dl/proof.html, ?file=dl/proof.html, and ?file=/dl/proof.html And the file exists, but it doesn't want to notice it. Link to comment Share on other sites More sharing options...
Ingolme Posted October 9, 2011 Share Posted October 9, 2011 Please modify the download.php file to point to the directory you wish to load the files from. Link to comment Share on other sites More sharing options...
cclloyd9785 Posted October 9, 2011 Author Share Posted October 9, 2011 And where is that? I looked at the file but am not sure, and I don't want to break it. The directory is /dl (With / being the root of my site, (full directory is /www/zxq.net/c/c/l/cclloyd/htdocs/dl/) Link to comment Share on other sites More sharing options...
Ingolme Posted October 9, 2011 Share Posted October 9, 2011 Try to understand the PHP code. It's not very difficult to understand. I even put comments in it. Link to comment Share on other sites More sharing options...
cclloyd9785 Posted October 9, 2011 Author Share Posted October 9, 2011 Ive tried editing it, without success. And the help guides on the main site didn't help, assuming I looked at the right ones. Link to comment Share on other sites More sharing options...
Ingolme Posted October 10, 2011 Share Posted October 10, 2011 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 More sharing options...
cclloyd9785 Posted October 10, 2011 Author Share Posted October 10, 2011 See, I did that, changed it to _dl (directory of files I want to be able to be downloaded, and the php file. And as the download link I typed "http://cclloyd.zxq.net/_dl/download.php?file=proof.html" So did I do something wrong, or am I missing something? Link to comment Share on other sites More sharing options...
Ingolme Posted October 10, 2011 Share Posted October 10, 2011 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 More sharing options...
Vertex21 Posted September 11, 2014 Share Posted September 11, 2014 (edited) 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 September 11, 2014 by Vertex21 Link to comment Share on other sites More sharing options...
Ingolme Posted September 12, 2014 Share Posted September 12, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now