The PHP Function Fseek
There are a lot of ways to process files in PHP. Classic functions like fread might serve you well much of the time, but sometimes you might want to work more intimately with the file itself. The php function fseek can help you do just that.
Fseek() is a function that allows you to move the file pointer for the specified file stream in a number of different ways. The first parameter is the file pointer, and the second one is the offset that you wish to seek to. The third is the whence, which can be SEEK_SET (default), SEEK_CUR, or SEEK_END.
In the example below, we're reading a text file with lines of a fixed length. We're using fseek() to skip ahead by a fixed number of characters, which might be more efficient than simply reading the entire file and parsing it with fscanf, or even just splitting it and echoing it.
The next step in the example is to use rewind() to move the file pointer back to the beginning of the file, and then we'll call fwrite(), which will replace that first letter "n" in the middle of the line with an "o". Finally, fseek() is called again with 2 as the offset and SEEK_CUR, which moves us two bytes ahead in the file.
It's worth noting that this method of working with files might not work with all streams - for example, any streams that were opened in append ("a" or "a+") mode will give undefined results when used with fseek().