The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Download a file from a remote server, then split the file

G'day

I've currently got no internet connection and need to download some linux cds (the LFS liveCD to be exact). So unable to download it all in one go, or install applications on a public computer I thought about hacking around with PHP again, download the file from the remove sever to my PHP server, split the file, then download it in bits from my server.

I can't really think of a more elegant solution, and I haven't touched PHP in years, so this sounds like a fun PHP hack job. Any idea's where I should start ?

I actually wrote a file-splitter in PHP a couple years back - needed to get around a 2MB limit on e-mail attachments. Anyway, this is how I would approach your problem:
1) Open a file handle to the remote server. fopen
2) Open a file handle to a local file.
3) Read up to a megabyte at a time. fread
4) Write the read bytes to the local file. fwrite
5) When the local file reaches a certain size (whatever size you want your chunks to be), fclose that file and fopen another one.

Remember that you'll likely also want to set_time_limit(0) and ignore_user_abort (the latter because I doubt you'll want to keep your browser open to your server the whole time, especially since your browser will likely assume the operation has timed out and terminate it). As you develop this, I'd recommend testing it on smaller files as you go so you don't discover after downloading the entire ISO that you screwed up royally. You'll also need to write something that will put the chunks back together on your local machine, but that should be trivial.