Pages

Saturday 7 May 2011

Web Space Management System - File Management - Part 3

This is part 3 of the Web Space Management System blog series, in this blog post I will be covering the remaining functionality for the application to be complete.

The first thing I did with this version is to copy all the code created in the first blog post in this series Web Space Management System - File Upload - Part 1 and modified the destination folder slightly first to have it located at a higher directory structure than the /htdocs folder thus making in unavailable for users through direct URL requests but also to build the path dynamically in the following format:

<USER_ROOT> / $_SESSION['username'] / <current user directory>
  • The USER_ROOT value is a hard coded parameter within the application which defines the directory on the server into which user directories are created.
  • the $_SESSION['username'] contains the actual username of the logged user
  • The <current user directory> value is actually stored on the session in $_SESSION['currPath'] and defines in what relative directory the user is currently in, to change directories we just alter this value and redirect back to the members.php page.
This structure allows requests for all file system operation to be relative to the user home~ directory and therefore prevents users from having access to other users or system directories.

Functionality included in this version:-

1. Creation of user home directories during registration
As mentioned in the previous blog post Web Space Management System - User Authentication - Part 2 the registration function had to be altered slightly to now create a home directory for each user as part of the registration process. The mkdir() function is used to create directories in php.

2. Directory listing/File system navigation
The members area has now been enhanced with file system navigation functionality, where the user can navigate through his assigned space in an "explorer" like fashion. both directories and files are click-able, clicking a directory navigates the user to that directory whilst a file will be downloaded once clicked.
The section highlighted in greed specifies the directory listing interface
The directory listing is achieved using the readdir() function, although not very clear in the current implementation I am already verifying whether a the listed element is a file or a directory using the is_dir() function to determine what action should be taken when the user clicks the filename - download or navigate, this same property can easily be used later when styling the layout to display a folder icon next to directories.

3. New folder creation

Apart from their home directory users are also allowed to create new folders within their area, a folder is always created as a child of the current directory the user is in.

The section highlighted in red specifies the folder creation interface
The create folder form posts to the createDirectory.php which will create a directory in the user's current path <USER_ROOT> / $_SESSION['username'] / <current user directory> using the mkdir() function
and then refreshes back to the member.php page which will give the user the illusion of an instant directory creation.

4. File download

As it is clearly shown in the above images, all files listed on screen are click-able (hyperlinks), all files redirect to download.php?f=<user relative path/filename>. Since the files cannot be accessed directly through a URL request (as they reside at a lower directory level then /htdocs) php will read the selected file and builds an http response with the contents of the file. Apart from added security for the user's content this also prevents the user from executing any script on the server such as php script as none of the downloads are routed through the interpreter but are served as is. Folders are also click-able but these requests are forwarded to the goto.php script which will reset the $_SESSION['currPath] to the selected path to allow the users to navigate though their content. 


5. File/Directory deletions

Any files/directories residing in the user's domain can be freely deleted by the user by simply clicking on the delete link available next to each list item. All deletions are routed through to the delete.php script which will check whether the $_GET['f'] - for file deletions or $_GET['d'] - for directory deletions have been set. Files are deleted using the unlink() function while directories are deleted using the rmdir() function.

In the next blog post, I will be going though the styling processes involved in converting this fully functional application to something more presentable.

References:
Calculate directory size using PHP - http://www.go4expert.com/forums/showthread.php?t=290

0 comments:

Post a Comment