I am working with file_get_contents
getting remote files and processing them. Unfortunately the files are quite large and I only need a small segment from each. I know exactly from what offset I would need to read and the length of it but reading the php manual it seems that this is possible with file_get_contents
only on local files.
Is there a method which doesn't download the whole file?
you need to use fopen
, fseek
and fread
function instead of file_get contents
here are the links to the documentation of
fopen
opens the file as a stream of bytes and you can seek into your desired position using fseek
and read as much byte as you need using fread
As far as I know there is no way to open a remote file and move the file pointer to a certain point. You need some kind of remote service that provides that feature for you.
Although fopen and fseek seem to work, there is a cullpit. Form the manual:
Not all streams support seeking. For those that do not support seeking, forward seeking from the current position is accomplished by reading and discarding data; other forms of seeking will fail.
As for implementing a service, there a numerous example. I like the rsync solution, that mirrors only changed blocks from a remote to a local machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With