Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:25:01+00:00 2026-06-12T12:25:01+00:00

How would I stop writing to the file and create a new file if

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:25:03+00:00Added an answer on June 12, 2026 at 12:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any danger if the rails html_escape function would stop escaping '&'? I
I have a search page where I would like to stop the user entering
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden
Question I need to make a short write to a file every 15 seconds
I'm using emacsformacosx.com and would like to stop the Meta_R (right meta, or right
I'm very new with javascript. I'm trying to create a tag using document.write (with
I'm writing an installer file in WiX with a custom action defined in a
I am writing a board game script and would like to use jQuery UI's
I'm writing a program in C that basically creates an archive file for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.