[PHP] Forced Download Using header()

2003-03-18 Thread Kevin Kaiser
I have a simple php download script that streams a non-web-accessible file
to the browser using this generally accepted method:

 header(Expires: 0);
 header(Cache-Control: private);
 header(Content-Type: application/save);
 header(Content-Length: .filesize($total) );
 header(Content-Disposition: attachment; filename=.$file_Path);
 header(Content-Transfer-Encoding: binary);
 $fh = fopen($total, rb);
 fpassthru($fh);

While it works, this is fairly useless in my situation where files range
from 5mb to over 100mb, because due to fpassthru() or readfile() or whatever
method used to stream the data to the recipient, the 'save as'/'open' dialog
doesn't open until the entire file has been downloaded. This is very
impractical since the user has no clue how much has been downloaded / how
much longer is left, not to mention that on very large files (~75mb) apache
will actually freeze up and become unresponsive to all users (sending cpu
usage to 100%) for nearly 10 minutes or more, assumedly because it is still
reading the file (regardless of whether the 'stop' button has been clicked
or not).

With the last 2 lines commented out (fopen() and fpassthru()), the save-as
dialog opens instantly.. is there any way fopen/fpassthru() could be delayed
until after the user chooses to open or save the file ? How would you guys
go about handling large file downloads while keeping the files themselves
non-web-accessible (aka not a direct link/redirector)?

Any help would be appreciated.
-KevinSync


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Forced Download Using header()

2003-03-18 Thread Ernest E Vogelsinger
At 11:16 18.03.2003, Kevin Kaiser said:
[snip]
   I have a simple php download script that streams a
 non-web-accessible file
to the browser using this generally accepted method:

 header(Expires: 0);
 header(Cache-Control: private);
 header(Content-Type: application/save);
 header(Content-Length: .filesize($total) );
 header(Content-Disposition: attachment; filename=.$file_Path);
 header(Content-Transfer-Encoding: binary);
 $fh = fopen($total, rb);
 fpassthru($fh);

   While it works, this is fairly useless in my situation where files
 range
from 5mb to over 100mb, because due to fpassthru() or readfile() or whatever
method used to stream the data to the recipient, the 'save as'/'open' dialog
doesn't open until the entire file has been downloaded. This is very
impractical since the user has no clue how much has been downloaded / how
much longer is left, not to mention that on very large files (~75mb) apache
will actually freeze up and become unresponsive to all users (sending cpu
usage to 100%) for nearly 10 minutes or more, assumedly because it is still
reading the file (regardless of whether the 'stop' button has been clicked
or not).

   With the last 2 lines commented out (fopen() and fpassthru()), the
 save-as
dialog opens instantly.. is there any way fopen/fpassthru() could be delayed
until after the user chooses to open or save the file ? How would you guys
go about handling large file downloads while keeping the files themselves
non-web-accessible (aka not a direct link/redirector)?
[snip] 

Disclaimer - this is just an idea, I've never dealt with downloading that
big files.

If apache freezes because the file it's reading is too big to be handled
I'd suggest to try a chunked approach, not using fpassthrough or a single
file read. The reason is that for every system I/O the operating systems
get a chance to switch process context, and to allow cooperative multitasking.

Would go something like that:

define('CHUNKSIZE', 8192);
$fp = fopen($file, 'r') or die (Cannot open $file);

header(Expires: 0);
header(Cache-Control: private);
header(Content-Type: application/save);
header(Content-Length: .filesize($total) );
header(Content-Disposition: attachment; filename=.$file_Path);
header(Content-Transfer-Encoding: binary);
   // --
while ($buffer = fread($fp, CHUNKSIZE)) {
echo $buffer;
}
fclose($fp);

You may need to experiment with the size of the chunks to keep up an
acceptable transfer rate. If you still encounter big holdups for other
processes you might consider using usleep() every 10th chunk or so, but use
your calculator to check how that would extend the overall transmission
time. For example, for a 100MB file, going to usleep for 50 msec after each
9kb chunk would mean that your process will sleep for 1.82 hours (!!) until
the file is delivered...


-- 
   O Ernest E. Vogelsinger
   (\)ICQ #13394035
^ http://www.vogelsinger.at/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Forced download??

2001-09-13 Thread Zhu George-CZZ010

Is there a way to do the forced download?  i.e., the browser can't read the file 
online(no matter whether it is .html or .txt file), instead, when he clicks the link, 
he will be asked to download it.

Is there a way to do that?

Thanks ahead!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Forced download??

2001-09-13 Thread Alexander Skwar

So sprach »Zhu George-CZZ010« am 2001-09-13 um 17:46:13 -0500 :
 Is there a way to do that?

Yes, send a bogus filetype, maybe something like
application/x-download.

PS: Please keep your lines below ~72 characters.

 Thanks ahead!

Yep.

Alexander Skwar
-- 
How to quote:   http://learn.to/quote (german) http://quote.6x.to (english)
Homepage:   http://www.digitalprojects.com   |   http://www.iso-top.de
   iso-top.de - Die günstige Art an Linux Distributionen zu kommen
Uptime: 23 hours 49 minutes

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]