I'm not sure I see the benefit to that approach?d11wtq wrote:I personally would use fread() iteratively so you only pull a small amount of data from the file into memory at any one time.
Are you trying to avoid taxing the systems memory limit by iteratively consuming memory instead of all at once?
I can see a few draw backs to that approach, but most importantly is memory segmentation or allocation of non-contiguous memory blocks. There are times when you might want to do that, like when reading a massive file which you may conditionally stop reading half way through based on some factor. But when dealing with image files like OP has suggested. I think it's best to load into memory right off the hop.
Either using get_file_contents or fread with a filesize.
Memory mapping in this case would make additional sense as the file is being requested a lot, so the additional over head in using system file operations like fread, fwrite would likely cause more problems.
Using system functions, this is how a file is typically read into memory:
1) Process reads file
2) Data moved from disk to buffers in Kernel address space
3) Memory pages are then copied into processes user space
That switch from user mode to kernel mode 'may' cause a context switch which is something you really want to avoid, especially if it's happening many times like in the case of an image request.
So personally, I'd say file_get_contents() is the better choice...but ultimately it boils down to personal choice.