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;
?>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