How would I stop writing to the file and create a new file if the string would make the file size bigger then 1 MB and break loop when 4th file is needed.
$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;
}
}
UPDATE
Please can some one explain why the filesize is always the same?
Thanks
Here is what I ended up with, thought I would share it if any one else is up late and scratching their heads.
I figured out why
filesize()would not update$sizewhen in a loop (1st post), you must callclearstatcache()before eachfilesize()used.PHP Manual
filesize()Note: The results of this function are cached. See
clearstatcache()for more details.Hope this helps any one.