>>72
> $max_buf = $size - $now_size if $size - $now_size < $max_buf;
>
> これで $now_size が $size より小さければ $max_buf は0になります。

ちがう。if文が偽を返しただけだ。