PDA

View Full Version : Force Download



Halting
01-09-2006, 08:34 PM
Hi,

How can I force a download for the following file extensions:
gif, jpg, jpeg, png, psd, bmp, tiff, dib, tif, jpe, jfif, swf and txt.

I'm having trouble finding a code that will support all of them.

Thanks in advance,
Oli

Josh-H
01-09-2006, 08:38 PM
What do you mean? Force a download for those exntesions :s

alexxxxx
01-09-2006, 08:38 PM
If you try and force an image download, it will just simply open in the browser right?

Halting
01-09-2006, 08:41 PM
That's what I thought. Because if you load an exe it will force it's download, but if you load an image it will show the image. There must be some way of forcing the download of forcing "Save Image As...".

alexxxxx
01-09-2006, 08:47 PM
That's what I thought. Because if you load an exe it will force it's download, but if you load an image it will show the image. There must be some way of forcing the download of forcing "Save Image As...".

I don't think so, because I think the Save Image As... feature is implimated by the browser rather than any script being able to access that function as all browsers are coded differently.

Colin-Roberts
01-09-2006, 08:47 PM
lol mostly impossible anymore to force downloads cause it will always come up with open with:
or save to: window in firefox and it'll always come up to were you want to save it too.

alexxxxx
01-09-2006, 08:49 PM
lol mostly impossible anymore to force downloads cause it will always come up with open with:
or save to: window in firefox and it'll always come up to were you want to save it too.

That's what he means, he wants that.

Yeah
01-09-2006, 09:47 PM
Ah i do not actually think this is possible all i can suggest is saving it as a zip :)

Halting
01-09-2006, 09:50 PM
My website is an image, text and flash host (www.urload.com). If I created a zip for every file that's been uploaded it would eat up my bandwith a lot more.

Yeah
01-09-2006, 09:51 PM
Hm then i don't think its possible, try www.google.com :)

Halting
01-09-2006, 10:06 PM
I have a zillion times. I don't need it urgently, I just thought I could give my visitors the ability to download files from our database which have been uploaded from other users.

Yeah
01-09-2006, 10:13 PM
Hmm no sorry.

You could always put like 'To Download This Image - Right click on the image and press save as'

Halting
01-09-2006, 10:21 PM
That's my alternative :)
Although, I did plan to disable right click to put a stop to multiple uploads (which will eat my bandwith) through right click >> refresh.

Oracle:
02-09-2006, 03:36 AM
Perhaps use .htaccess to re-direct to the image file.

Xen
02-09-2006, 07:31 AM
cAN YOU force a download?

I thought it only let you upload specific extensions?

Splinter
02-09-2006, 09:45 AM
zip the images up..

Halting
02-09-2006, 10:15 AM
Perhaps use .htaccess to re-direct to the image file.
What do you mean? If you redirected a window to the image it would just display the image rather than download.

zip the images up..
I've already said why I'm not doing that.

Hitman
02-09-2006, 10:19 AM
Put it in a zip, and it'll ask if you want to download it?

edit: read post, hmm, I do not know, I will have a look.

Oracle:
02-09-2006, 11:56 AM
Try modifying this. I got it off hotscripts.


<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title> Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

Sygon.
02-09-2006, 01:12 PM
Hm then i don't think its possible, try www.google.com (http://www.google.com) :)

Its actually possible, heres the code:




<?
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>



Save that as process.php

Then to download the file put..


<a href="process.php?file=picture.jpg">Download JPG image</a>

:)

source: http://www.sometricks.com/2006/04/23/download-pdf-jpg-gif-directly-from-the-web-browser

Halting
02-09-2006, 01:51 PM
I love you Sygon. (l)

Want to hide these adverts? Register an account for free!