Page 1 of 1

forced download: headers problems -- I think

Posted: Tue Oct 26, 2004 11:58 pm
by beli
This problem is driving me crazy. I am trying to force a binary file download that will work with and without cookies being enabled at the client. I also need to be able to track sessions. Here is the code:

Code: Select all

<?php
session_cache_limiter('public');  // session.cache_limiter = no-cache  in php.ini
session_start();

// starting session above, because Sesion Vars will be used in code here
// ... session/db code here

$filename='setup.exe'; // the file name at client's end
$file_to_download='/path/somefile.exe'; // the file name on the server -- THIS IS ABSOLUTE PATH


header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file_to_download));

$user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]);
if ((is_integer (strpos($user_agent, "msie"))) && (is_integer (strpos($user_agent, "win")))) 
{
  header( "Content-Disposition: filename=".$filename.";" );
} else {
  header( "Content-Disposition: attachment; filename=".$filename.";" );
}

header("Content-Transfer-Encoding: binary");
header("Content-Description: File Transfer");

$data = file_get_contents($file_to_download);  	// could use readfile() 
echo $data;  

?>
If I remove session_start(), things work fine. But I need session_start() because there will be some code (e.g., user authentication and determining file paths) that uses session vars before I go to pushing the file to the output buffer.

With session_start() in the code, things work fine when cookies are enabled at a client. An expected "Open/Save" window appears, etc.

When cookies are disabled at a client my session tracking changes to transient SID (i.e., PHPSESSID appears as GET var in URL), of course. Now this is where things get weird. With cookies disabled the file download is not happening. The open/save window is not poping up, the progress bar is slowly moving in the browser, until the page eventually timeouts (30 sec in my case), after which "Page not found" displays.

The files I am trying to download are large executables -- about 5MB. I also tried testing with just dumping some data to the browser window (by removing all heaader() lines listed in the code above). Still, the file reading seems to be stack and times out.

This behavior is consistent for both IE and Mozila/Firefox.

I suspect some issues with headers that are somehow implicitly set by session_start(). Any ideas?

Thanks,

Beli

Posted: Wed Oct 27, 2004 1:21 am
by m3mn0n
Warn all users to enable cookies and/or forward users to an instructional page showing how to do this if the download is attempted..

Posted: Wed Oct 27, 2004 1:46 am
by beli
Yes, I am doing that right now. But there's got to be an explanation and hopefully a fix for this.

Beli

Posted: Thu Feb 03, 2005 4:54 pm
by myleow
I did a similar code and was able to download the files that i made, the only problem i have encountered is HTTPS + session_start() gives me problem in I.E.