note 103591 added to function.ftruncate

From: Date: Thu, 21 Apr 2011 15:31:52 +0000
Subject: note 103591 added to function.ftruncate
Groups: php.notes 
Request: Send a blank email to php-notes+get-179046@lists.php.net to get a copy of this message
If you want to ftruncate but keep the end: <?php function ftruncatestart($filename,$maxfilesize){ $size=filesize($filename); if ($size<$maxfilesize*1.0) return; $maxfilesize=$maxfilesize*0.5; //we don't want to do it too often... $fh=fopen($filename,"r+"); $start=ftell($fh); fseek($fh,-$maxfilesize,SEEK_END); $drop=fgets($fh); $offset=ftell($fh); for ($x=0;$x<$maxfilesize;$x++){ fseek($fh,$x+$offset); $c=fgetc($fh); fseek($fh,$x); fwrite($fh,$c); } ftruncate($fh,$maxfilesize-strlen($drop)); fclose($fh); } ?> It will not just cut it but search for a newline so you avoid corrupting your csv or logfiles. But I don't know if you will stress the reading head of your drive. ;) ---- Server IP: 212.124.37.9 Probable Submitter: 195.243.98.54 ---- Manual Page -- http://www.php.net/manual/en/function.ftruncate.php Edit -- https://master.php.net/note/edit/103591 Del: integrated -- https://master.php.net/note/delete/103591/integrated Del: useless -- https://master.php.net/note/delete/103591/useless Del: bad code -- https://master.php.net/note/delete/103591/bad+code Del: spam -- https://master.php.net/note/delete/103591/spam Del: non-english -- https://master.php.net/note/delete/103591/non-english Del: in docs -- https://master.php.net/note/delete/103591/in+docs Del: other reasons-- https://master.php.net/note/delete/103591 Reject -- https://master.php.net/note/reject/103591 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179046) next »