December 28th, 2010
Jennifer Web Design Tags, ,

Dropbox PHP Stream Wrapper

Editor Note: This is the first in a new series of Blog Posts from guest posters.  This post comes from a programmer we have worked with for years Mr. Mark Hulka.

I’ve been poking around for a while looking for a good, simple backup solution for MySql databases. I had been debating using ¬†Cloudfiles because of the API and cheap storage. As I was about to pull the trigger and sign up for a Cloudfiles account then a friend of mine, the illustrious Ben Jones, sent me a link to Dropbox.

I was a little leary of it at first because I had not heard of it but after a few minutes of research and after trying it out I was hooked! I love this thing. Dropbox allows you to share files very easily using it’s software that creates a Dropbox folder in My Documents in Windows. You just drag and drop and BOOM the files are online ready to share. Well “BOOM” depends on your internet connection but it’s pretty darn fast. Not only could I share files easily but I also learned that Dropbox does file versioning. Hmmm…could I use this?

The magic came when I found out Dropbox had an API and a PHP library created by a Dropbox community user. Using the API and the library I created a PHP stream wrapper. A stream wrapper let’s you use all of the normal file commands using custom functions. So, for instance, I can now use ‘include’, ‘file_get_contents’, or ‘copy’ on Dropbox files. Now, the API is really meant for creating mobile applications but anyone can sign up for an API key and start developing. Your application isn’t widely available until you get it approved by Dropbox. Because I just wanted a backup system I could care less about making the application public.

The stream wrapper is by no means a finished, or even polished, product but it worked out for what I needed to do so feel free to use it or make it better! I’ve included the stream wrapper and a backup script. The backup script is simple:

  1. Does a dump of the MySql database
  2. Gzips it
  3. Copies to Dropbox

In order to use it:

  1. Sign up for a Dropbox account
  2. Sign up for an API key by creating an application in the Developers section
  3. Download the PHP library
  4. Enjoy!

With the stream wrapper you can do things like:

Copy a file from your server to your Dropbox account

	

copy('my_local_file.tar.gz', 'dbox://whatever@test.com:mypassword@a_dropbox_folder/filename.tar.gz?key=my_dropbox_key&secret=my_dropbox_secret');

	

Grab the contents of a Dropbox file

	

$str = file_get_contents('dbox://whatever@test.com:mypassword@filename.txt?key=my_dropbox_key&secret=my_dropbox_secret');

	

Include a Dropbox file

	

include('dbox://whatever@test.com:mypassword@filename.php?key=my_dropbox_key&secret=my_dropbox_secret');

	

Download Source Code from my Dropbox Account

-In addition to programming Mark manages several Golf Game web sites