Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Can I use fseek to control file size?
September 8, 2004, 6:03 pm
rate this thread
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");
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?
Re: Can I use fseek to control file size?
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
-----END PGP SIGNATURE-----
- » shell script - to not change working directory to that of the script
- — Previous thread in » PHP Scripting Forum