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);
Last edited by Taschen; 04-19-2007 at 03:35 AM.
Reason: replied to wrong post
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);
<?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");
?>
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
<?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");
?>
Bookmarks