FTRUNCATE在开头留出空间

I want to truncate a file with each loop and write again. So, I am using below code.

However, what is happening here is I am getting some extra space at the beginning in each loop. There is no space as 1st time no space is being added (also I tried trim function).

Can you please help me?

$file = fopen("progress/".$_POST['fname'].".txt","w");
while($row=mysql_fetch_assoc($result)){
    updateDB($row['movieid'],trim($row['tempname']));
    ftruncate($file,0);
    fwrite($file,round(100*$complete++/$tot));
}
fclose($file);

OUTPUT: (something like this)

11
  20
    30
      40

You forgot to move the file pointer back to the beginning. ftruncate() does not do this for you. Writing to an advanced position fills the preceding bytes with NULL. Use fseek().