Imagine a situation where your application has to store and retrieve files on the web (i.e. not on a local filesystem). You have many options - you may upload them to FTP server, e-mail them, use some file hosting services like Dropbox, upload files using a HTML form, use WebDAV server. Finally you may mount some remote filesystem like NFS.
All of these options are valid, but they all carry certain amount of requirements that may not always be met:
- To use FTP, you need to set up a remote FTP server, have an implemented FTP client in your language of choice and the ability to open FTP connections on the system you're using.
- To use e-mail you need to be able to handle POP3 and SMTP protocols and have a mail server set up.
- WebDAV, although convenient, is hard to set up in the first place. The protocol itself takes some time to implement.
- Using any other web application like Dropbox requires you to have a client for their services and you need to accept the licence restrictions.
- HTML form - an excelent choice. If you're doing the uploads manually, you may write a simple script in minutes - but what if you want to upload files automatically (e.g. in a batch script)? You need to make a HTTP request with form and the file within encoded, you have to deal with mime-types, encoding file contents etc. Not really fast to implement.
- Mounting remote filesystem is impossible on a shared Linux server, or Windows server.
HTTP File server to the rescue. This small little fellow, written in PHP5 is a simple REST-oriented file server with minimal requirements:
- PHP 5 (5.2 I suppose)
- web server (Apache will do)
- writable directory (this is where your files will be stored)
This is for the server part. For the client part you only need to be able to do HTTP GET and HTTP POST requests, so you're good with just wget in a batch script (or .NET application, or Ruby, PHP, Java - pretty much anything nowadays can form HTTP requests).
# store file on server - use HTTP POST wget --post-file=file_to_send.txt http://server/index.php/path/to/store/file.txt -O - # retrieve file - use HTTP GET wget http://server/index.php/path/to/store/file.txt
That's pretty much it. The server is so simple, it doesn't (yet?) offer even the ability to list directory contents. All it does is store files and retrieve them.
Download HTTPFileServer and take a look for yourself. Your comments are welcome. The project is BSD licensed.