forced download: headers problems -- I think

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beli
Forum Newbie
Posts: 2
Joined: Tue Oct 26, 2004 11:16 pm
Location: Pittsburgh, PA

forced download: headers problems -- I think

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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..
beli
Forum Newbie
Posts: 2
Joined: Tue Oct 26, 2004 11:16 pm
Location: Pittsburgh, PA

Post by beli »

Yes, I am doing that right now. But there's got to be an explanation and hopefully a fix for this.

Beli
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post 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.
Post Reply