It's better in that you're not doing a line-based approach any more. What I would say is you're getting a little too specific right now. If you look at the linked reference, you'll see:
What you can take from that, is your file will essentially a repeating sequence of "\xFF\x??<marker>\x????<length>\x??...<data>". The two exceptions to worry about right away are the start of image and end of image markers, they don't have a length and data component.
As such, you should start by being able to parse that repeating sequence. Don't worry about parsing what exactly is contained inside the data, just get the individual blocks. In pseudo code that'd be something like:
while (!feof($file)){
$marker = findNextMarker($file); //Scan the file until you find a 0xFF?? value, return the ??.
if ($marker !== 0xD8 && $marker !== 0xD9){
$length = parseLength($file); //Read two bytes, convert them to a integer length value
$data = fread($file, $length - 2); // -2 because the encoded length includes the two bytes read above.
}
}
I did this, and have a simple script that does like I said above, just parses out the different blocks and shows a hex dump of their data. I'll share eventually, but I want to see what you come up with after taking the above into consideration first.