Click to See Complete Forum and Search --> : force a file download problem
kl1980
01-14-2008, 06:01 PM
In my PHP code I am using the header() function to force the output file download. But the output renders to the browser itself and does'nt prompt the download window for the user to dowload the output file. I am also using include_once funtion to include some functions. If I remove the include_once function then the download window is prompted, but if the include_oce function is there then the output is rendered on to the browser itself. Could anyone please let me know what is mistake......
Thanks! in advance.....
felgall
01-14-2008, 07:48 PM
Is the include before or after the header statement? If before then maybe it is outputting something to the page and so the header will then not work. A single space is enough to do it.
kl1980
01-15-2008, 03:24 PM
Yes! the include is placed before the header statement. SO what do you mean by a single space is enough to do it. Could you please explain.
Thanks!
knightman
01-15-2008, 06:32 PM
try this.. works great for me!
somedomain.com/download.php?file=filename.type
"download.php"
<?php
$filename = $_GET['file'];
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>Download Script</title><body>ERROR: download file NOT SPECIFIED using download.php file</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>Download Script</title><body>ERROR: File not found using download.php file</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();
?>
let me know if works 4 u :cool:
etard
03-09-2009, 02:59 PM
try this.. works great for me!
somedomain.com/download.php?file=filename.type
"download.php"
<?php
$filename = $_GET['file'];
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>Download Script</title><body>ERROR: download file NOT SPECIFIED using download.php file</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>Download Script</title><body>ERROR: File not found using download.php file</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();
?>
let me know if works 4 u :cool:
to revive this topic... I need to get IE to force the download of a file when the user clicks the "download" button and not open it. but in this script, I am curious is there actually a place that you put the file name you are looking to be forced to download? if so, where does the file name/link go?
it seems if I just take this script and load it up on the server, and then point to it and add ?path=abc.exe in the url, it does not work. I get an error "ERROR: File not found using download.php file"
so, what am I missing? am I using it correctly? I would think I just have the button, attach a link to the button of myserver.com/download.php?file=abc.exe
etard
03-09-2009, 07:29 PM
okay.. I solved it.. seemed I had to have the files on my server (?file=abc.exe) and a full URL to the file was a no go (?file=myserver.com/abc.exe)... very odd.
anyway, now the issue is that this approach does NOT seem to work if you try to use it to automatically download files using a meta refresh approach on the page load.
so, for example, this works:
myserver.com/download.php?file=abc.exe
this will NOT work:
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=myserver.com/download.php?file=abc.exe"/>
any ideas why? If you try to use the file download on page load, IE gives you that dreaded "to help protect...." and stops the download from happening until you interact. BUT if you use the former - which uses the same link - it works perfect???
knightman
03-09-2009, 08:21 PM
correct!
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=myserver.com/download.php?file=abc.exe"/> will not work
there are 2 ways of doing that with javascript (both works for me)
one way:
<html>
<head>
<script>
function redirect()
{
window.location.href = "download.php?file=abc.exe";
}
</script>
</head>
<BODY onload="javascript:setTimeout('redirect()', 0000)">
</</BODY> >
</html>
and the other way;
<html>
<head>
</head>
<BODY>
<script type="text/javascript">
function delay()
{
window.location="download.php?file=abc.exe";
}
setTimeout(delay,0000)
</script>
</BODY>
</html>
by the way, if your download.php file, and your abc.exe are in the same dir,
there's no need for the "myserver.com" part (as in "?file=myserver.com/abc.exe")
if the script in on your root dir, and if your .exe is on a sub dir, ("programs" for
example) then point the script like this:
myserver.com/download.php?file=programs/abc.exe
i wish this helps! :)
etard
03-09-2009, 08:43 PM
I opted to use your latter option (as the former uses an onLoad event and I already have an onLoad event so just kept it easy):
<script type="text/javascript">
function delay()
{
window.location="download.php?file=abc.exe";
}
setTimeout(delay,0000)
</script>
BUT, it does not work... again, same as with the meta refresh, it will cause IE to stop the download and not "force the download".
so again, same issue.. this forced download php works perfect IF I just have
download.php?file=abc.exe
but the moment you seem to put the php files link into something like your code window.location="download.php?file=abc.exe"; or a meta refresh META HTTP-EQUIV="Refresh" CONTENT="0; URL=download.php?file=abc.exe" it fails to work and IE does not do the forced download. and a "forced" download is the whole key goal here. the forced download code here only seems to work if it is the href link off a button or text link, but NOT as part of a call from other code like meta refresh or a javascript call????
etard
03-10-2009, 05:24 PM
by the way, if your download.php file, and your abc.exe are in the same dir,
there's no need for the "myserver.com" part (as in "?file=myserver.com/abc.exe")
if the script in on your root dir, and if your .exe is on a sub dir, ("programs" for
example) then point the script like this:
myserver.com/download.php?file=programs/abc.exe
i wish this helps! :)
okay... here is an interesting note. I got the system to work perfect.
that is, until I add in the URL to the file and the URL is off site at a special site that hosts the files as they are large executable exe files.
so... like I said above, if I have something like this which shows that the executable file is in the same directory as the download.php, all works perfect!
download.php?file=programs/abc.exe WORKS
BUT if the executable is offsite, it does not work and breaks and says the "ERROR: File not found"
download.php?file=www.server.com/programs/abc.exe BREAKS
any idea why this happens and if a way to fix it?
knightman
03-11-2009, 07:13 PM
is "server.com" your own webspace, or a domain other than yours?
the script and the files to be downloaded must be on ur own hosting.
yoursite.com/download.php?file=programs/abc.exe
WILL ALWAYS WORK even with the javascript functions posted before.
BUT
yoursite.com/download.php?file=www.server.com/programs/abc.exe
won't.
nor even this;
yoursite.com/download.php?file=www.yoursite.com/programs/abc.exe
etard
03-11-2009, 08:34 PM
server.com is NOT our web server. it is a file hosting platform that Amazon provides.
"the script and the files to be downloaded must be on ur own hosting."
well, that explains it, and what I suspected. but the question is... why? is it something in the php that makes that limitation and is that 'something' changeable in the code, or is it a server thing and no way around it?