View Full Version : Error handling force download.. :(

11-29-2007, 05:00 AM
I need to provide download for php file on my site.

I use this script:

session_cache_limiter('none'); //*Use before session_start()

$file = 'file.php';
_Download("hiddendir/".$file, $file);

function _Download($f_location,$f_name){
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($f_location));
header('Content-Disposition: attachment; filename=' . basename($f_name));

but give me this error: Warning: Cannot modify header information - headers already sent by (output started at ....

then I use force download scripts from http://www.zubrag.com/scripts/ and http://www.gowondesigns.com/?page.getfile

they works well, then i added 'php' => 'application/octet-stream' to the configuration of both script. but it doesn't work.

could somebody help me pls...

thank you.

11-29-2007, 06:08 AM
the headers already sent error is because you cant run the header() command after any text of anything has been sent. Maybe a white space at the beginning of the file? or the end of a configuration file?


11-29-2007, 09:55 AM
The portion of the error message that you did not post states where the output was started that is preventing the headers from being sent.

Read the error message or post the whole error if you want specific help with the problem.

11-29-2007, 04:04 PM
oh, i got it, the problem is because i inserted that code after another code.
is header session only works in one single php file and cannot inserted into another file?

how to solve the problem, because i need this script between my another code.

thank you.

11-29-2007, 04:18 PM
You can put that code anywhere you want, as long as no output has been sent yet.

Output is anything echoed, printed or any characters outside of PHP blocks.

11-29-2007, 04:19 PM
You cannot insert the actual download code in web page. It must be in its own .php file and there must be a link to that file placed on a web page. You click the link to request the page that causes the file to be downloaded.

11-29-2007, 05:37 PM
oh I see now.
thank you for the enlightment and forgive me for my lackness. :D