Can I use fseek to control file size?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    #1

    Can I use fseek to control file size?

    I'm trying to control the size of a log file. When it gets more than 5k
    oversize, I attempt to trim it thusly:

    $fcap = intval(($dval * 0.0108))*1024; // what it should be
    $fsize = filesize($file) ; // what it is
    $oversize = $fsize - $fcap;

    if ( $oversize > 5120 ) // 5k oversize
    {
    $fp = fopen($file,"r+ ");
    fseek($fp, -$fcap, SEEK_END);
    $redux = fread($fp, $fsize);
    $fp = fopen($file,"w" );
    fwrite($fp, $redux);
    }

    The problem is the file is getting wiped out - reduced to 0k

    Doesn't the fseek statement say "move the pointer to the end of the file,
    then back up by the amount of $fcap" ? Then, data from the pointer down is
    read into $redux, and $redux overwrites $file ?

    Am I missing something? Why is my log file getting wiped out?


  • Chris

    #2
    Re: Can I use fseek to control file size?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    deko wrote:
    [color=blue]
    > I'm trying to control the size of a log file. When it gets more
    > than 5k oversize, I attempt to trim it thusly:
    >
    > $fcap = intval(($dval * 0.0108))*1024; // what it should be
    > $fsize = filesize($file) ; // what it is
    > $oversize = $fsize - $fcap;
    >
    > if ( $oversize > 5120 ) // 5k oversize
    > {
    > $fp = fopen($file,"r+ ");
    > fseek($fp, -$fcap, SEEK_END);
    > $redux = fread($fp, $fsize);
    > $fp = fopen($file,"w" );
    > fwrite($fp, $redux);
    > }
    >
    > The problem is the file is getting wiped out - reduced to 0k
    >
    > Doesn't the fseek statement say "move the pointer to the end of the
    > file,
    > then back up by the amount of $fcap" ? Then, data from the pointer
    > down is read into $redux, and $redux overwrites $file ?
    >
    > Am I missing something? Why is my log file getting wiped out?[/color]

    Hi,
    First, the first fopen() should probably use mode "r".

    Second, you're not closing the file after the first use, so this might
    screw things up. Put an fclose($fp); after the fread() line.

    Third, shouldn't the fread() line be reading $fcap bytes, not $fsize
    bytes? Reading $fsize bytes means trying to go beyond EOF (which
    according to the documentation isn't a problem), while reading $fcap
    bytes means you get *exactly* what you want.

    If nothing works, try some error checking on every single line inside
    the loop: they *all* return values.

    Chris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFBP8hmgxS rXuMbw1YRAlW9AK DVhi8Fk8jDEqEuO 7i6nSHdtKcWfQCe JEdN
    pMAKFD6k8E9J3Ch ZizeAxxw=
    =yT4W
    -----END PGP SIGNATURE-----

    Comment

    • deko

      #3
      Re: Can I use fseek to control file size?

      > First, the first fopen() should probably use mode "r".

      I think I remember trying this but it did not work for some reason. I'll
      try it again.
      [color=blue]
      > Second, you're not closing the file after the first use, so this might
      > screw things up. Put an fclose($fp); after the fread() line.[/color]

      That part of the code was omitted.
      [color=blue]
      > Third, shouldn't the fread() line be reading $fcap bytes, not $fsize
      > bytes? Reading $fsize bytes means trying to go beyond EOF (which
      > according to the documentation isn't a problem), while reading $fcap
      > bytes means you get *exactly* what you want.[/color]

      perhaps...
      [color=blue]
      > If nothing works, try some error checking on every single line inside
      > the loop: they *all* return values.[/color]

      Actually, I got it working - the issue was a file lock routine that was not
      part of my post.

      Thanks for the reply.


      Comment

      • Drazen Gemic

        #4
        Re: Can I use fseek to control file size?

        On Thu, 09 Sep 2004 04:09:57 +0000, deko wrote:
        [color=blue]
        > Actually, I got it working - the issue was a file lock routine that was not
        > part of my post.[/color]

        I think that it is a good idea to call 'clearstatcache ' before 'filesize'.

        DG

        Comment

        Working...