php - efficent way to get and remove first line in file
- by Marco Demaio
Hello,
I have a script that each time is called gets the 1st line of a file. Each line is known to be exactly of the same length (32 alphanumerci chars) and terminates with a "\r\n".
After getting the 1st line, the script removes it.
Now I do in this way:
$contents = file_get_contents($file));
$first_line = substr($contents, 0, 32);
file_put_contents($file, substr($contents, 32 + 2)); //+2 because we remove also the \r\n
Obvioulsy it works, but I was wondering if there could be a smarter (or more efficent) way to do this???
In my simple solution I basically read and rewrite all the file just to take and remove the 1st line.
Thanks!