Re: [PHP] How to count transfered kBytes in File-Download

2009-01-05 Thread ceo

> When mentioning the RAM usage problem, one might consider calling

> flush() after each echo, just to make sure that they don't run over

> PHPs memory limit.



Oh yeah.



Make sure you've run through and cleared all ob_buffers for any kind of 
non-HTML output, specifically for file downloads.



You really don't want your web server buffering up a potentially large 
streamable content just because you've got ob_auto_start or whatever in 
php.ini, or some kind of main controller calling ob_start() or whatever.



I know a lot of folks swear by ob_start and its benefits, but I mostly find it 
annoying as a general "solution"...



Didn't even think of that, as I only use ob_start() when I *really* want it, 
not as a general fix-up.



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



Re: [PHP] How to count transfered kBytes in File-Download

2009-01-05 Thread Jim Lucas
c...@l-i-e.com wrote:
>> echo fread($HANDLER, $FSIZE);
> 
> This is your problem child right here...
> 
> Sucking in an entire OGG File to RAM, for a large OGG file, will be quite 
> painful.
> 
> And, on a busy server, even moderate size files will be problematic.
> 
> You could probably relieve a lot of stress and keep full-size downloads just 
> by doing:
> 
> define('CHUNK_SIZE', 2048);
> define('DOWNLOAD_LIMIT', 10*1024);
> 
> while (!feof($HANDLER)){
>   echo fread($HANDLER, CHUNK_SIZE);
> }
> 
> To answer your original question, you would abort this loop partway though, 
> using some kind of counter *= CHUNK_SIZE and compare it to DOWNLOAD_LIMIT.
> 
> 

ceo, you made me realize a problem with yours and my example also.

When mentioning the RAM usage problem, one might consider calling flush() after 
each echo,
just to make sure that they don't run over PHPs memory limit.

-- 
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] How to count transfered kBytes in File-Download

2009-01-05 Thread ceo

> echo fread($HANDLER, $FSIZE);



This is your problem child right here...



Sucking in an entire OGG File to RAM, for a large OGG file, will be quite 
painful.



And, on a busy server, even moderate size files will be problematic.



You could probably relieve a lot of stress and keep full-size downloads just by 
doing:



define('CHUNK_SIZE', 2048);

define('DOWNLOAD_LIMIT', 10*1024);



while (!feof($HANDLER)){

  echo fread($HANDLER, CHUNK_SIZE);

}



To answer your original question, you would abort this loop partway though, 
using some kind of counter *= CHUNK_SIZE and compare it to DOWNLOAD_LIMIT.



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



Re: [PHP] How to count transfered kBytes in File-Download

2009-01-02 Thread Jim Lucas

Michelle Konzack wrote:

Hello *,

currently I have a function:

[ '/usr/share/tdphp-vserver/includes/02_functions.inc' ]

function fncPushBinary($type='show', $file, $mime='') {


Outside your function, or in, I don't care, define a variable/constant that will be used as a buffer 
size.


$buffer = 1024;


  if ( is_file("$file") ) {
if ($mime == '') {
  $mime=exec("file -i -b $file");
}
$STRG="; filename=\"" . basename($file) . "\"";


Don't do the following...

> $FSIZE=filesize("$file");




if ($type == 'show') {
  header("Content-Disposition: inline" . $STRG );
} else {
  header("Content-Disposition: attachment" . $STRG);
}
header("Content-Type: " . $mime);
$HANDLER=fopen("$file", r);


Then here, do this...

$current_size = 0;
while ( !feof($HANDLER) ) {
$current_size += $buffer;
echo fread($HANDLE, $buffer);
}

Now, do what you want with $current_size

Maybe have a variable that you check it against that contains the users allow 
amount of transfer...

$current_size = 0;
while ( !feof($HANDLER) && $current_size < $allowed_limit ) {
$current_size += $buffer;
echo fread($HANDLE, $buffer);
}

Hope this gets you leading down the right path...


echo fread($HANDLER, $FSIZE);
fclose($HANDLER);
exit();
  } else {
$ERRNO=2;
$ERRTX=sprintf( T_gettext("The requested binary file does not 
exist.%s"), $file );
fncError($ERRNO, $ERRTX, $ERRPAGE);
  }
}



which I want to modify it to count the kBytes a user download and  block
after a definde amount...

(there are too many peoples in the world which want to suck anything for
free and unlimited and no one cares who pay the stuff...  even if I have
100 TByte free traffic on one of my bigger servers)

My problem is now

1) how to count partial downloads
2) how to allow users to make partial downloads

any suggestions?

Oh, I should note, that the URL's looks like:

/?what=music&where=h§ion=foo&name=I_love_php5.ogg

and the input to the function is already sanitized.

Thanks, Greetings and nice Day/Evening
Michelle Konzack
Systemadministrator
24V Electronic Engineer
Tamay Dogan Network
Debian GNU/Linux Consultant





--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare


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