0
ファイルの書き込み中にファイルが指定されたサイズを超えていることを確認する必要があります。 私は指定されたサイズに達すると、私は書き込みを停止し、例外をスローする必要があります。書き込み中にGzipとBzip2ファイルのオーバーフローを検出する方法はありますか?
$handle = fopen($filename, 'wb');
while (true) {
// its work
if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) {
throw SizeOverflowException::withLimit(BYTE_LIMIT);
}
fwrite($this->handle, $string);
}
fclose($handle);
または私は独立して使用されるバイト
$handle = fopen($filename, 'wb');
$used_bytes = 0;
while (true) {
if ($used_bytes + strlen($string) > BYTE_LIMIT) {
throw SizeOverflowException::withLimit(BYTE_LIMIT);
}
fwrite($this->handle, $string);
$used_bytes += strlen($string);
}
fclose($handle);
例Gzipでの書き込み数えることができる:Bzip2でのために同様
$handle = gzopen($filename, 'wb9');
while (true) {
// not work
// fstat($handle) === false
//if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) {
// throw SizeOverflowException::withLimit(BYTE_LIMIT);
//}
gzwrite($this->handle, $string);
}
gzclose($handle);
:
通常ファイルの例
$handle = bzopen($filename, 'w');
while (true) {
// not work
// fstat($handle) === false
//if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) {
// throw SizeOverflowException::withLimit(BYTE_LIMIT);
//}
bzwrite($this->handle, $string);
}
bzclose($handle);
私はこの場合にfstat()
が動作しない理由を理解していますが、どうすればこの問題を解決できますか?
ここで、圧縮されていないモードで使用されるバイト数のみを計算できます。
**素晴らしいアイデア。ありがとうございます。**残念ながら、私はPHP 5.6のサポートが必要ですが、依然としてBzip2(( – ghost404