I've built an app where users can download files. It works fine with more than one downloads if it is a public download site. The problem is when using session_start() (to check if user logged in), which causes else the site/page to lock and not allow more than one downloads at a time.
Here's the steps that would replicate the issue:
-click on a large file to download (a little pop up window containing php script) shows and disappears and download dialog starts
-while large file is downloading, click on another file to try to download (in firefox, the little window pops up, but it doesn't close, it locks.. so after the big file downloads, it gives a time out message, in IE, occasionally it will keep the pop up open, waiting to finish downloading, then allow you to save)... OR try to refresh the page, it locks.
My code is something like...:
PHP Code:
$dir = "/"; //whatever path here;
session_start();// calling this will cause problems, but if left out, file downloads is public
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }//for IE
header('Pragma: public');//
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');//IE
header('Last-Modified: '.date('j/n/Y h:i A',@filemtime($file)));
header('Cache-Control: private',false);//
$file=$dir.$_GET["filePath"];
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-length: ".@filesize($file));
header("Content-disposition: attachment; filename=" .''.@basename($file)."");
header('Connection: close');//
readfile("$file");
The link to the file would be something like...
http://localhost/thescript.php?fileP...oads/demo2.txt Also note that this script is so users do not download file with a direct link.
Attempted solutions:
I've tried several things.
-I looked at
http://php.net/manual/en/function.se...he-limiter.php
radu dot rendec at ines dot ro wrote a comment on 02-Sep-2005 10:04 which addressed the same exact problem.. and noted a solution with ob_start()...
Quote:
"I played about an hour with the download and sessions. yes, to work you'll need session_cache_limiter("must-revalidate"); but this BEFORE session_start() if you want that your download start [IE problem]. Hope someone will need this someday
====
yes, somebody has needed this today 
situation: trying to make a session based download management system complete with user login system that requries an authorized user to download some files, and hide all such files from non-authorized users. the user login, download center, and content management system of the site are all tied in to each other, making troubleshooting this headering stuff a headache.
problem: files being served are not accessible thru the regular site, since they are above the htdocs folder in apache, and so headering the file is required, and sessions do not work well with files being headered to the browser.
solution: the download center uses ob_start("");, then session_cache_limiter("must-revalidate");, before the session_start();, then everything works well.
thank you very much! i was resorting to using a cookie to control this before because i could not figure out how to tie in sessions to the system before!"
|
So I attempted to us ob_start() before session start and ob_flush() after headers. e.g.
Code:
ob_start();
session_start()
... few lines of header()....
ob_flush();
I'm not sure if I used that correctly. I tried ob_end_clean and a few combination. I haven't used ob functions that much and never have for something like this.
-I've also tried moving around where session_start gets called and tried to put it between header calls and after calls, but that isn't ideal, because the app needs to be called before file download executes to check if user is logged in.
-I've tried using set_time_limit(0); on that page, and although it does allow download, it is not user friendly, (it keeps the little window open and makes the user wait until the file finishes before it disappears, and shows file download dialog.. instead of queuing the downloads)
-I've tried session_cache_limiter("nocache"); before calling session_start()
I haven't found other solutions or other reports of this issue. Any suggestion is greatly appreciated. Thank you!