Hello,
I'm not great at figuring this one out. I need to make 3 links force download the files to your computer in every single browser. Excluding old browsers like IE6.
Please see my links below I need to apply this too.
<?php
$fullsize_image = wp_get_attachment_image_src( $image['id'], 'full-size' );
$large_image = wp_get_attachment_image_src( $image['id'], 'large' );
$medium_image = wp_get_attachment_image_src( $image['id'], 'medium' );
?>
<div class="download-buttons">
<a href="<?php echo $fullsize_image[0]; ?>" title="Image Size: <?php echo $fullsize_image[1] . ' x ' . $fullsize_image[2]; ?>" class="download">Full Size</a>
<a href="<?php echo $large_image[0]; ?>" title="Image Size: <?php echo $large_image[1] . ' x ' . $large_image[2]; ?>" class="download">Medium Size</a>
<a href="<?php echo $medium_image[0]; ?>" title="Image Size: <?php echo $medium_image[1] . ' x ' . $medium_image[2]; ?>" class="download">Web Size</a>
</div>
Preferably I would like PHP to do this. I do not want to add stuff to my .htaccess.
I don't mind jquery or javascript to apply the php to these links. But as long as it works.
First person with working answer - or cleanest/simplest solution get the full amount.
Many Thanks
Josh
Daniel Yoen answers:
this is simple version download.php
<?php
$file = $_GET['file'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public'); //for i.e.
header('Pragma: public');
ob_clean();
flush();
readfile($file);
exit;
?>
then, your download link
<?php
$fullsize_image = wp_get_attachment_image_src( $image['id'], 'full-size' );
$large_image = wp_get_attachment_image_src( $image['id'], 'large' );
$medium_image = wp_get_attachment_image_src( $image['id'], 'medium' );
?>
<a href="http://example.com/download.php?file=<?php echo $fullsize_image; ?>">Download</a>
rewrite your dynamic link here : http://www.generateit.net/mod-rewrite/
hope this help
Josh Cranwell comments:
Well that was easy.
Does it work it every browser? And file type?
Josh Cranwell comments:
I get this in safari...
[[LINK href="http://i.imgur.com/bLy0A.png"]]http://i.imgur.com/bLy0A.png[[/LINK]]
Daniel Yoen comments:
yes, for all browser(check new download.php revision), and yes for for all file type, but it would be better if the specific file type, like 'application/pdf', because 'application/octet-stream' is mean 'other' file type :-)
It's simple version, advance version have spesific file type, support partial download etc, you can see here : http://php.net/manual/en/function.readfile.php
Thank you
Daniel Yoen comments:
Ops sorry, file location is correct?
Dbranes answers:
Hi, here is very simple way with a template_redirect hook (paste into function.php) :
add_action('template_redirect','download_images');
function download_images() {
$download=$_GET['download'];
$download=esc_attr( $download);
if (strlen($download)>0) {
$filetype = wp_check_filetype($download);
if($filetype['ext']=="jpg"){
header("Content-type: application/image",true,200);
header("Content-Disposition: attachment; filename=".$download);
header("Pragma: no-cache");
header("Expires: 0");
exit();
}
}
}
Josh Cranwell comments:
Hi,
I have other links on the page, that I want to open normally, just my links above I need to download automatically.
I also have other file types too on other download links.
Thanks
Dbranes comments:
this code pice only force download on links with the download GET parameter, fx:
http://example.com/?download=http://example.com/wp-content/uploads/2012/06/test.jpg
Here is a usage example:
<?php
$fullsize_image = wp_get_attachment_image_src( $image['id'], 'full-size' );
$large_image = wp_get_attachment_image_src( $image['id'], 'large' );
$medium_image = wp_get_attachment_image_src( $image['id'], 'medium' );
// modify this:
$download_link="http://example.com/?download=";
?>
<div class="download-buttons">
<a href="<?php echo $download_link.$fullsize_image[0]; ?>" title="Image Size: <?php echo $fullsize_image[1] . ' x ' . $fullsize_image[2]; ?>" class="download">Full Size</a>
<a href="<?php echo $download_link.$large_image[0]; ?>" title="Image Size: <?php echo $large_image[1] . ' x ' . $large_image[2]; ?>" class="download">Medium Size</a>
<a href="<?php echo $download_link.$medium_image[0]; ?>" title="Image Size: <?php echo $medium_image[1] . ' x ' . $medium_image[2]; ?>" class="download">Web Size</a>
</div>
Here is a support for jpg/gif/png
add_action('template_redirect','download_images');
function download_images() {
$download=$_GET['download'];
$download=esc_attr( $download);
if (strlen($download)>0) {
$filetype = wp_check_filetype($download);
// restriction on jpg/gif/png filetypes
if($filetype['ext']=="jpg" or $filetype['ext']=="gif" or $filetype['ext']=="png"){
header("Content-type: application/images",true,200);
header("Content-Disposition: attachment; filename=".$download);
header("Pragma: no-cache");
header("Expires: 0");
exit();
}
}
}
Dbranes comments:
ps: here is a version with
header("Content-type: ".$filetype['type'],true,200);
instead, i.e.
add_action('template_redirect','download_images');
function download_images() {
$download=$_GET['download'];
$download=esc_attr( $download);
if (strlen($download)>0) {
$filetype = wp_check_filetype($download);
// restriction on jpg/gif/png filetypes
if($filetype['ext']=="jpg" or $filetype['ext']=="gif" or $filetype['ext']=="png"){
header("Content-type: ".$filetype['type'],true,200);
header("Content-Disposition: attachment; filename=".$download);
header("Pragma: no-cache");
header("Expires: 0");
exit();
}
}
}
Josh Cranwell comments:
Very very cool.
However it renames my files. How is it doing that exactly?
Many Thanks
Josh Cranwell comments:
Also this is what I get in safari...
[[LINK href="http://i.imgur.com/2SJw1.jpg"]]http://i.imgur.com/2SJw1.jpg[[/LINK]]
Seems to work in..
Chrome: 22.0.1
FF: 16.0.2
IE: 8 and compatibly mode (have not tested 9-10 but presume they will be ok)
But Safari 5.05 i get that warning.
Thanks
Dbranes comments:
here is a version that should not rename your files
(and it only allows files from your own hostname)
add_action('template_redirect','download_images');
function download_images() {
$myhost="example.com";// <--- change this
$download=$_GET['download'];
$download=esc_attr( $download);
if (strlen($download)>0) {
$filetype = wp_check_filetype($download);
// only allow files from my own host
if(parse_url($download,PHP_URL_HOST)!=$myhost){
return;
}
//restrict filetypes to jpg/gif/png
if($filetype['ext']=="jpg" or $filetype['ext']=="gif" or $filetype['ext']=="png"){
$download_file= basename($download);
header("Content-type: ".$filetype['type'],true,200);
header("Content-Disposition: attachment; filename=".$download_file);
header("Pragma: no-cache");
header("Expires: 0");
exit();
}
}
}
Josh Cranwell comments:
Actually I've realised even with the new function that all the files that are being downloaded are <strong>Zero KB</strong>
And contain nothing, just empty files.
The renamin part works - how every nothing is actually downloading as such. Just a blank file.
Sorry my bad for not spotting this first time round.
Thanks
Dbranes comments:
This seems to work in Safari/Chrome/FF on Mac and Chrome/IE8 on PC ;-)
add_action('template_redirect','download_images');
function download_images() {
$myhost="example.com";// change this
$download=$_GET['download'];
$download=esc_attr( $download);
if (strlen($download)>0) {
$filetype = wp_check_filetype($download);
// only allow files from my own host
if(parse_url($download,PHP_URL_HOST)!=$myhost){
die("wrong host");
}
//restrict filetypes to jpg/gif/png
if($filetype['ext']=="jpg" or $filetype['ext']=="gif" or $filetype['ext']=="png"){
$download_file_name= basename($download);
header('content-type: application/octet-stream');
header("Content-Disposition: attachment; filename=".$download_file_name);
header("Pragma: no-cache");
header("Expires: 0");
readfile($download);
exit();
}
}
}
Josh Cranwell comments:
Awesome, this seems to work great!
Thanks very much!!!
Navjot Singh answers:
I have written about it here: http://nspeaks.com/force-file-downloads-instead-of-opening/ You can append the image links as an argument to the download.php file and they will always download.
Edit the links like this:
http://www.domain.com/download.php?file=<?php echo $large_image[0]; ?>
Josh Cranwell comments:
Ok so with the path, do I do it like this?
http://www.domain.com/download.php?file=http://www.domain.com/wp-content/uploads/2012/10/file.jpg
Navjot Singh comments:
Yep. See my edited answer. Remove these two lines if it doesn't work:
$len = filesize($sourceFile); - line no 12
header("Content-Length: ".$len); - line no 58.
Josh Cranwell comments:
Nither works, I just get... 404 File not found!
See my output mark up below...
<div class="download-buttons">
<a href="http://domain.com/download.php?file=http://domain.com/wp/wp-content/uploads/2012/10/MCR2L2012A_AB.jpg" title="Image Size: 3648 x 2736">Full Size</a>
<a href="http://domain.com/download.php?file=http://domain.com/wp/wp-content/uploads/2012/10/MCR2L2012A_AB-1920x1440.jpg" title="Image Size: 1920 x 1440">Medium Size</a>
<a href="http://domain.com/download.php?file=http://domain.com/wp/wp-content/uploads/2012/10/MCR2L2012A_AB-1024x768.jpg" title="Image Size: 1024 x 768">Web Size</a>
</div>
I put the download.php in my www root so it can be found. I think it is being found due to the message appearing.
Michael Caputo answers:
try this
<?php
$fullsize_image = wp_get_attachment_image_src( $image['id'], 'full-size' );
$large_image = wp_get_attachment_image_src( $image['id'], 'large' );
$medium_image = wp_get_attachment_image_src( $image['id'], 'medium' );
?>
<div class="download-buttons">
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $fullsize_image[0]; ?>');" title="Image Size: <?php echo $fullsize_image[1] . ' x ' . $fullsize_image[2]; ?>">Full Size</a>
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $large_image[0]; ?>');" title="Image Size: <?php echo $large_image[1] . ' x ' . $large_image[2]; ?>">Medium Size</a>
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $medium_image[0]; ?>');" title="Image Size: <?php echo $medium_image[1] . ' x ' . $medium_image[2]; ?>">Web Size</a>
</div>
Josh Cranwell comments:
Tried this, does not work.
My source copied direct...
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $fullsize_image[0]; ?>');" title="Image Size: <?php echo $fullsize_image[1] . ' x ' . $fullsize_image[2]; ?>" class="button dark left">Full Size</a>
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $large_image[0]; ?>');" title="Image Size: <?php echo $large_image[1] . ' x ' . $large_image[2]; ?>" class="button dark left">Medium Size</a>
<a href="javascript:void(0);" onclick="document.execCommand('SaveAs',true,'<?php echo $medium_image[0]; ?>');" title="Image Size: <?php echo $medium_image[1] . ' x ' . $medium_image[2]; ?>" class="button dark left">Web Size</a>
Michael Caputo comments:
Can you paste the output?