I have to implement a simple file download client in PHP capable of downloading large files as well as resuming them.
Is there a way i can download large files (>700 MB) in PHP and still have my PHP memory limit to 128M ? I’m guessing this has to do with writing to a file pointer. Any clue on which file handling functions to use ? there are so many. I am guessing fopen, flock, (fwrite,fgets,fread), fclose. Or should i use cURL ?
How do i resume downloads which are broken ? Script execution timeout, user stopping script, remote server timeout etc. ?
This should be possible using cURL by setting the
CURLOPT_FILEandCURLOPT_RESUME_FROMoptions. I’m not sure whether cURL will overwrite the file or append to it, also whether it’ll buffer the file in memory or write it straight to disk. You may have to do some tests there.If you want more control over the whole process, you can use
fsockopento create a raw connection to the server you’re downloading from and write to and read from this connection using the normalfreadandfwritefunctions. You’d have to send (fwrite) the correct HTTP headers to the connection to initiate the transfer—most importantly theRangeheader for resuming transfers—and then read a few bytes usingfread, then write those to a file and repeat until the transfer is complete.