views:

48

answers:

1

Hello,

I have been assigned a new task by the client, a document sharing application to be made as per MVC design pattern. Here are the requirements:

  • Uploads and downloads files with a browser

  • Store the document in db if that are more secure documents else store on the directory with options of password enabled or accessible with out password

  • Every user will be have own document catalog / workspace from where he can be able to share documents with other users as well. and public shared area to share and upload files

  • Super admin will be able monitor the file upload logging for monitoring purpose.

I have rough idea but I would really like to know your thoughts about above points especially what is in bold up there.

The third point above is most important and I am not sure where to start from and how to go about logging the uploads. Thanks for your suggestions.

I am basically asking for implementation details about the third and fourth points.

+2  A: 

Here is how I implement this with CakePHP and it works nicely. First, I make sure my app code sits above the public html directory so that it is not exposed to the web. So basically, the only files the users have direct access to is the index.php file, the css/js and image files.

Then, I update my file management model to save and delete files on the fly:

function beforeSave() {
    extract($this->data['Upload']['file']);
    if(isset($name) and !empty($name)) {
        $filename = time().'-'.$name;
        if ($size && !$error) {
            move_uploaded_file($tmp_name, APP . 'media/files/' . $filename);
            $this->data['Upload']['file'] = $filename;
            $this->data['Upload']['name'] = $name;
            $this->data['Upload']['file_type'] = $type;
        }
    } else {
        // remove the photo so it is not updated
        unset($this->data['Upload']['file']);
    }
    return parent::beforeSave();
}

function beforeDelete() {
    $data = $this->read(null, $this->id);
    if( is_file( APP . 'media/files/' . $data['Upload']['file'])) {
        unlink(APP . 'media/files/' . $data['Upload']['file']);
    }
    return true;
}

This will manage the file upload and put all of the naming information into the database. And since the app/media/files directory is not accessible to the web, I do not have to protect the directory. It means that no matter what file the user wants, they have to access it from the website.

Then, all you have to do is make sure the model table has a "shareable" flag for the user to indicate that the file is accessible to the world, and then anyone can see the file and download it.

cdburgess