views:

30

answers:

2

I have written a WordPress plugin, and I am trying to find the best way of having the program check for updates on my server, and downloading them automatically at the user's request. This will basically be used to download extra features that I don't want to put into the WordPress repository due them not having the GNU license.

I've coded the program to check for updates, what I am not sure about is the download part.

Should I use file_get_contents() and then fwrite() to write the results to a directory? I'm sure that's a way of doing it, but I am looking for the most cross-server safe way of doing this because the plugin is used by thousands of people on different setups, although they are all PHP, and most of them are PHP5+. I am afraid certain hosts may have some sort of security preventing me from doing this. Would cURL be a better option?

Thank you.

+2  A: 

You can use file_put_contents, which is much more efficient than loading everything into memory with file_get_contents and then calling fwrite. The second parameter accepts a stream resource, which you can open with fopen or (if you want to get around allow_url_fopen restrictions, fsockopen).

Therefore, the simplest option that uses only core functions (no curl extension, etc) and works in almost evry configuration is:

Artefacto
Looks good, I'll test it out and post on here the results.
Andy
If your downloadable file is password protected somehow (basic HTTP auth, form-based login, etc...), then you'll have to use CURL. `file_get_contents()` is very basic and won't be able to handle any kind of authentication/login.
Marc B
@Marc The http wrapper (which file_get_contents will use) can do basic authentication out of the box and you can implement digest authentication manually (granted, with some effort) if you need to. Form based logins can also be handled by the http wrapper since you can send arbitrary post data and arbitrary headers by creating the appropriate context. See http://www.php.net/manual/en/context.http.php
Artefacto
+1  A: 

Don't re-invent the wheel! WordPress has an HTTP API for this exact purpose!

Check out the codex for the essentials - http://codex.wordpress.org/HTTP_API

You can also browse the source in wp-includes/class-http.php

TheDeadMedic
You are correct, I found those functions shortly after posting this question. WP has a number of functions available for downloading headers, files, extracting them, etc. Unfortunately there isn't much documentation available on this apart from PHPDoc entries within the files.
Andy