Conditions | 6 |
Paths | 5 |
Total Lines | 29 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
20 | public static function ftruncatestart(string $filename, $maxfilesize) : bool |
||
21 | { |
||
22 | if (!FileHelper::fileExistsSafe($filename)) { |
||
23 | return false; |
||
24 | } |
||
25 | $size = filesize($filename); |
||
26 | if ($size === false || ($size < $maxfilesize * 1.0)) { |
||
27 | return false; |
||
28 | } |
||
29 | $maxfilesize = $maxfilesize * 0.5; //we don't want to do it too often... |
||
30 | $fh = fopen($filename, "r+"); |
||
31 | if ($fh === false) { |
||
32 | return false; |
||
33 | } |
||
34 | ftell($fh); |
||
35 | fseek($fh, -$maxfilesize, SEEK_END); |
||
36 | $drop = fgets($fh); |
||
37 | $offset = ftell($fh); |
||
38 | for ($x = 0; $x < $maxfilesize; $x++) { |
||
39 | fseek($fh, $x + $offset); |
||
40 | $c = fgetc($fh); |
||
41 | fseek($fh, $x); |
||
42 | fwrite($fh, $c); |
||
43 | } |
||
44 | ftruncate($fh, $maxfilesize - strlen($drop)); |
||
45 | fclose($fh); |
||
46 | |||
47 | return true; |
||
48 | } |
||
49 | } |
||
50 |