phpoverriding

Override data without truncating the file


When I make a growing file in PHP, I usually use the file_put_contents function with FILE_APPEND and LOCK_EX flags.

Now, I have a growing video file that I play with mplayer.

To avoid that the file becomes too big, I want to overwrite data that I've already played without truncating the file. In bash, there is a simple function to do this:

dd if=/dev/zero of=myoutputfile conv=notrunc count=1 bs=128k

explained here http://en.wikipedia.org/wiki//dev/zero

Is there a way to obtain the same thing in PHP?


Solution

  • How about this:

    $h = fopen("myoutput", "r+"); // '+' means also for writing without truncating
    fseek($h, $wherever_you_need);
    fwrite($h, $data); // overwrite
    fclose($h);