/    Sign up×
Community /Pin to ProfileBookmark

force a file download problem

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…..

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@felgallJan 15.2008 — 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.
Copy linkTweet thisAlerts:
@kl1980authorJan 15.2008 — 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!
Copy linkTweet thisAlerts:
@knightmanJan 16.2008 — try this.. works great for me!

somedomain.com/download.php?file=filename.type


"download.php"

[code=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();

?>
[/code]


let me know if works 4 u ?
Copy linkTweet thisAlerts:
@etardMar 09.2009 — try this.. works great for me!

somedomain.com/download.php?file=filename.type


"download.php"

[code=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();

?>
[/code]


let me know if works 4 u ?[/QUOTE]



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
Copy linkTweet thisAlerts:
@etardMar 10.2009 — 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???
Copy linkTweet thisAlerts:
@knightmanMar 10.2009 — 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:
[code=php]
<html>
<head>

<script>
function redirect()
{
window.location.href = "download.php?file=abc.exe";
}
</script>
</head>


<BODY onload="javascript:setTimeout('redirect()', 0000)">


</</BODY> >
</html>

[/code]




and the other way;
[code=php]

<html>
<head>
</head>
<BODY>


<script type="text/javascript">
function delay()
{
window.location="download.php?file=abc.exe";
}
setTimeout(delay,0000)
</script>

</BODY>
</html>

[/code]


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! ?
Copy linkTweet thisAlerts:
@etardMar 10.2009 — 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>

[U]BUT, it does not work[/U]... 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 [I]window.location="download.php?file=abc.exe";[/I] or a meta refresh [I]META HTTP-EQUIV="Refresh" CONTENT="0; URL=download.php?file=abc.exe[/I]" it fails to work and [B]IE does not do the forced download.[/B] 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????
Copy linkTweet thisAlerts:
@etardMar 10.2009 — 

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! ?[/QUOTE]


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 [B]WORKS[/B]

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 [B]BREAKS[/B]

any idea why this happens and if a way to fix it?
Copy linkTweet thisAlerts:
@knightmanMar 12.2009 — 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
Copy linkTweet thisAlerts:
@etardMar 12.2009 — 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?
×

Success!

Help @kl1980 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...