Cesar D. Rodas, web development. Technology news. PHP, MySQL, Apache, C, Bash, ASM

Remote File system in PHP

Hello to every one, you may be surprised by the title of this post and you may be wondering why a remote file-system in PHP? If around there exist many projects that do that, why re-invent the road?

Well, there are sometimes that you are not the root and you need to share files, especially when you had shared hosting or simple, you wanna became the competition of Amazon S3 (I don’t know why the first example is a bit more possible than the seconds =) ).

This idea came to me after finish my Amazon S3 class and publish on phpclasses, and I saw was a good idea, to provide a Web service to share disk information on others servers, that is a basic Remote file-system because you use the store of other remote computer.

Basic Architecture

Basic PHP-FEP Architecture

This is the basic architecture, A client request a file (directory, or a chunk of a file) for read, write, create or delete. The server has an authentication callback function that controls is a given user, password has enough privileges for the request operation, if has, process, if not, refuse it.

With this simple module the we have a secure file-system, and I say file sistem becaus ethe client implements a Stream Wrapper, that means that you can do operations over files (fopen,fwrite,fclose,mkdir,rmdir, unlink,stat,fstat, fseek, and others commands) as if you are doing on a local file, adding “fep://” in the beginng.

Bellow there is an example of an FEP client that creates a folder(/php) and create and write a new file (/php/readme)

<?php
require(“FEP_client.php”);
set_time_limit(100);
define(‘FEP_USER’,‘cesar’); /*username*/
define(‘FEP_PASS’,‘rasec’); /*password */
define(‘FEP_SERVER’,‘http://server.com/fep-example.php’);
mkdir(“fep://php”);
file_put_contents(“fep://php/readme”,“this is a text to save in a file”);
?>

Here is an example of function server to handle that request:

<?php
require(“FEP_server.php”);

/**
* Creating a FEP server, and passing as a parameter
* the callback function that will check the users
* permissions.
*/
$server = new FEP_server(‘auth’);
/*
* Working directory, PHP should be the owner of this folder or at
*
read write permission. If this property is not set,
* by default it is pwd();
*/
$server->setWorkingDir(‘/foo/bar/repository’);
/* start server, handle queries */
$server->start();


function auth($user, &$password, $action, $path) {
/* in this example, user cesar, with password rasec could do everything*/
if ($user == “cesar”) {
$password=“rasec”;
return
true;
}
return
false;
}
?>

We know that the Authentication module is independent of the FEP-server class, that allow to manage the users and permissions easily, it can be store in a database and manage a more granulary permission system ( user:foo can only read files, and list one directory).

Also the client had a in-memory file buffer, that means, that files are write on client main memory and is synchronized to the server when the operation is finished or when a fflush() is called, then those chunks are transferred to server. For read, something similar happened, if the request part of the file doesn’t exist on the cache buffer, this is request to the server (usually a chunk of 16kb) and it is store on memory, then future request will only be search on client main memory, for save of bandwidth. Also when a fflush or fclose is close, are synchronized to the server only those chunks that had been changed. Those which are the same are not synchronized.

Future work

Well, there a lot of things to do, right now, the most urgent thing is to implement an MRU model for manage main memory spaces efficiently.

Where to download it

You can download FEP, client and server from here. This project is protected under a BSD license that allow closed sources to include this code for free, this is for allow commercial applications, or people who don’t want or can’t share their codes to use it.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. Digg del.icio.us StumbleUpon Technorati BlinkList Furl NewsVine Reddit

One Response to “Remote File system in PHP”

  1. Adam Says:

    One well thought out system. I always respect the programmers who take the time to draw up a diagram - especially a UML diagram!

Leave a Reply


Fatal error: Call to undefined function display_cryptographp() in /home/.schmuckie/saddor/cesarodas.com/wp-content/themes/first-spring-10/comments.php on line 94