如果字符串会使文件大小大于 1 MB 并在需要第 4 个文件时中断循环,我将如何停止写入文件并创建一个新文件。
$max_size = 1048576; // 1 MB
$max_files = 3;
$i = 1;
$loop = true;
$size = 0;
$x = 0;
while($loop)
{
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n";
$file = 'C:\xampplite\htdocs\moo\file_'.$i.'.tmp';
if(file_exists($file))
{
$size = filesize($file);
echo 'File exists with size: '.$size.'<br>';
}
else
{
$size = 0;
echo 'No file exists, size: '.$size.'<br>';
}
$fh = @fopen($file, 'a');
if( ! $fh)
{
echo 'Failed to write to temp file "'.$file.'".';
}
fwrite($fh, $str);
fclose($fh);
$x++;
if($x == 100)
{
break;
}
}
更新 请有人解释为什么文件大小总是相同的吗?谢谢