How can I force an image to be downloaded but not displayed in the browser window?
Printable View
How can I force an image to be downloaded but not displayed in the browser window?
How can I force an image to be downloaded but not displayed in the browser window?
Put this in the head of your page.
This will cache the image in your temporary internet files folder, but won't display it until you want to.Code:<script type="text/javascript">
var img1 = new Image();
img1.src = 'path/to/your/image.gif';
</script>
Opps, bit of a bobo, meant to reply to the guy who was asking about file download in php. I have no idea how you force a download using JS.
In PHP use header - use octet stream rather than file type (eg jpg)
This together with content disposition will correctly force download for a document/image etc.
Caution: absolutely do not allow the file to be choosen using $_GET['file'] without cleaning $_GET['file'] first.
PHP Code://set the content as octet-stream
header("Content-Type: application/octet-stream"); //
// tell the thing the filesize
header("Content-Length: " . filesize($download_path.$file));
// set it as an attachment and give a file name
header('Content-Disposition: attachment; filename='.$file);
// read into the buffer
readfile($download_path.$file);
See you posted this under js as well. Here's the PHP reply.
In PHP use header - use octet stream rather than file type (eg jpg)
This together with content disposition will correctly force download for a document/image etc.
Caution: absolutely do not allow the file to be choosen using $_GET['file'] without cleaning $_GET['file'] first.
PHP Code://set the content as octet-stream
header("Content-Type: application/octet-stream"); //
// tell the thing the filesize
header("Content-Length: " . filesize($download_path.$file));
// set it as an attachment and give a file name
header('Content-Disposition: attachment; filename='.$file);
// read into the buffer
readfile($download_path.$file);
This is what I use:
download.php:
usage:PHP Code:<?php
// Force download of image file specified in URL query string and which
// is in the same directory as this script:
if(!empty($_GET['img']))
{
$filename = basename($_GET['img']); // don't accept other directories
$size = @getimagesize($filename);
$fp = @fopen($filename, "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
}
}
header("HTTP/1.0 404 Not Found");
?>
HTML Code:<img src="/images/download.php?img=imagename.jpg" alt="test">
@haroon373,
Don't double post. If you can't get an answer in one, state it and open a different one. The two have been merged.
Hello,
I used this code and it works great.
http://gottweeters.com/follow-me-buttons
Thanks NogDog!