You cannot open a file for both reading and writing. In your code, you open the file for writing, therefore you cannot read it. And that's why $line2 does not contain any data...