RSS Feed

Peter Goodman's blog about PHP, Parsing Theory, C++, Functional Programming, Applications,

PHP5 / AJAX FTP Drag and Drop Uploader

Note: I no longer support or distribute the code for the drag & drop uploader. Some of the code (the main JavaScript and PHP) can be found here.

So over the weekend and up until today I've been having fun with the PHP5 framework that I'm working on. One particular system that I'm proud of is the layers system. As it sounds, it manages layers. For example, database abstraction currently has two layers: SQLite and MySQLi. There's nothing too interesting about that. What is interesting are my current file layers: System and FTP (planned: Amazon S3 and LDAP).

With these file layers I have made it stupidly easy to transparently transfer files from one layer to another (FTP->System, System->FTP). I was proud of these generally simple classes so I decided that I wanted to flex their muscles. I made a system to ftp uploader. I added a dash of jQuery and a stupid amount of javascript and now I have myself a drag and drop uploader!

I made a quick video of it in action. Sorry for the poor quality / ugly demo text. When I have money in my bank account, I will invest in a better screen capture program, I swear! Take a look at a short video of the FTP AJAX uploader in action here.

By the way, the total amount of PHP (in the controller) to handle all that is just over 200 lines (including comments). Here's the jist of how files are transferred:

// get the file layer fos the source files
$from_system = $this->getLayer('file', $from_layer);

// get the source files
foreach($files as $key => $file_name) {
    $files[$key] = $from_system->find($file_name, FileAbstract::FILE);
}

// transfer them to the destination directory, note: write() will accept
// 3 different argument types: FileAbstract, Iterator, or a file path/name.
$this->getLayer('file', $to_layer)
     ->find($log["last_{$to_layer}_location"], FileAbstract::DIRECTORY)
     ->write(new ArrayIterator($files));

And here's how I list out files in a directory:

// get the layer
$file_system = $this->getLayer('file', $layer);

// find the directory, read the files from it, and set it
// as a template list
$response['files'] = $file_system->find($dir, FileAbstract::DIRECTORY)->read();

Pretty neat. The templates--using my own template engine based on this HTML parser--are equally sweet.


Comments


Comment