Tree view based file explorer for the web using jQuery.

This post assumes that the reader has a basic understanding of PHP, jQuery and jQuery $.post

Recently I had to implement a windows-esque tree view based file explorer for one of my projects at work. I ended up using this wonderful jQuery plugin to help me. In this blog I’ll be going through some basic code to get it working using PHP on the server side.

To whet your appetite, here’s what we’ll be achieving in this blog post – A complete tree view based file explorer that feeds from a folder on the server’s file system.

File tree view

So let’s get started!

To be able to render a proper tree using the aciTree plugin, we’ll need to send JSON from server in the following format –

Here’s the list of things that we’ll be doing –

  1. Server Side Code
  2. HTML
  3. JavaScript/jQuery code to communicate with the server.
  4. Basic event handling for the aciTree plugin.

1. Server side code

Let’s move onto the PHP code. Our job is to read the files from the specified folder and return a JSON structure that resembles what’s shown above. If you look closely you’ll observe that the JSON above is basically an array of objects of with properties as – id, label, inode, icon, open

The following class is exactly similar to that of the class received via JSON. We’ll be returning an array of NodeList objects from the server.

Now to read the list of files and folders from a location on the server –

Some code to handle the request that we will be making from the client side and then echoing the output in JSON format –

Okay so we have the PHP code in place. Next order of things –

  1. Server Side Code
  2. HTML
  3. JavaScript/jQuery code to communicate with the server.
  4. Basic event handling for the aciTree plugin.

2. HTML

The following HTML goes inside the body tag-

Next, the JavaScript/jQuery to get the aciTree plugin working. This should be put inside the document’s ready handler –

3. jQuery code to communicate with the server

4. Event handling for the aciTree plugin

And finally a simple event handler for the aciTree that is triggered whenever a node in the tree is selected –

You can find the whole code here and a working copy of the project here.

Leave a Reply