Question

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

Was it helpful?

Solution 2

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 $size when in a loop (1st post), you must call clearstatcache() before each filesize() used.

PHP Manual filesize()
Note: The results of this function are cached. See clearstatcache() for more details.

$start = microtime(true);
$max_size = 1048576; // 1 MB
$max_files = 3;
$file_num = 1;
$size = 0;
$lines = 0;
$total_lines = 0;

while(true)
{                    
        $file = '/path/to/file_'.$file_num.'.tmp';            
        $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";

        if($lines === 0)
        {
                if(file_exists($file))
                {
                        clearstatcache();
                        $size = filesize($file);
                        print("<pre>" . print_r('File exists with size = '.$size, true). "</pre>");
                }
                else
                {
                        $size = 0;
                        print("<pre>" . print_r('No file exists, size = '.$size, true). "</pre>");

                }
        }

        // add string length to size
        $size = ($size + strlen($str));

        if($size > $max_size)
        {
                print("<pre>" . print_r('Max file size exceeded for file '.$file_num.'. Total lines written '.$lines, true). "</pre>");
                $file_num++;
                $lines = 0;

                // escape loop after creating 3 files
                if($file_num > $max_files)
                {
                        break;
                }

                continue;
        }

        $lines++;
        $total_lines++;

        $fh = @fopen($file, 'a');

        if( ! $fh)
        {
                echo 'Failed to write to temp file "'.$file.'".';
        } 

        fwrite($fh, $str);
        //print("<pre>" . print_r('Writing to file: '.$file, true). "</pre>");
        fclose($fh);    
}

$end = microtime(true);

$time = $end - $start;
print("<pre>" . print_r('------------------------------------------------------------', true). "</pre>");
print("<pre>" . print_r('Total time: '.$time.' seconds.', true). "</pre>");
print("<pre>" . print_r('Total lines: '.$total_lines, true). "</pre>");

Hope this helps any one.

OTHER TIPS

Keep track of how much data you are writing out. Before you do the write, add the length of the text to the length you've already written out. If it reaches the limit, increment i, reset the count of bytes written out, and continue. If you are writing to existing files, get the file size using filesize().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top