1. Christian Salazar
  2. YiiFileManager

Source

YiiFileManager / README.md

Yii File Manager

Fileman is a framework for handling files attached to an given Identity, it can be commonly your current logged in user.

Fileman is designed as an abstract class, so many child classes can be built to satisfy business dependencies, like: Storing files in a disk file, in a user session, in a database, and so on.

In this case only one child class is provided: YiiDiskFileManager, created to store files in a disk.

Install the fileman component:

Register the fileman component into your config file.

'import'=>array(
    ...bla...
    'application.extensions.yiifileman.*',  // <<--HERE
),

'components'=>array(
    'fileman' => array(
        'class'=>'application.extensions.yiifileman.YiiDiskFileManager',
        'storage_path' => "/var/tmp/fileman",
),

Basic yii File Manager operations

$identity = Yii::app()->user->id; // or any IDentificator.

// ADD FILES
//
Yii::app()->fileman->add_files($identity,"/some/place/filename.mp3");
Yii::app()->fileman->add_files($identity,array("file1", "file2"));

// LIST FILES
//
foreach(Yii::app()->fileman->list_files($identity) as $fd)
    printf("file -> [%s] [%s] [%s]\n",
            $fd['id'],$fd['file_id'],$fd['filename']);

// QUERY THE REAL PATH FOR A GIVEN FILE ID

$real_path = Yii::app()->fileman->get_file_path($id, "18aac1a12");
// ..do what ever you want with this file...
printf("the real local path is: %s\n",$real_path);

// REMOVE FILES
//
Yii::app()->fileman->remove_file($identity, "file1");
Yii::app()->fileman->remove_file($identity, array("file1","file2"));

Yii File Manager Class Diagram

Class Diagram

Test Fileman in a Command Line utility.

  1. Create a file "protected/commands/Filemancommand.php" having this content (see below).

  2. Be sure you have the rigth config file in "protected/yiic.php"

  3. Due to this is a test, edit the source code (look at "PUT SOME FILE HERE") and edit the array to provide real files.

  4. test it running commands:

    # cd myapp
    # cd protected
    # ./yiic fileman
<?php
// save as:
//      protected/commands/FilemanCommand.php   
//
class FilemanCommand extends CConsoleCommand {
    public function actionIndex($cmd='list',$fileids=''){
        $id = "MYID";
        printf("\nFileman tester. ID=%s\n",$id);
        printf("usage:\n");
        printf("    --cmd=add\n");
        printf("    --cmd=list\n");
        printf("    --cmd=rem --fileids=129819,129982\n");
        printf("    --cmd=path --fileids=129819\n");
        printf("\n");
        if($cmd=='add'){
            // ADD
            //
            $file_ids = Yii::app()->fileman->add_files($id, 
                array("/var/tmp/file1","/var/tmp/file2"));      <---  PUT SOME FILES HERE

            foreach($file_ids as $file_id)
                printf("add_files -> file_id: %s\n",$file_id);
        }elseif($cmd=='list'){
            // LIST
            //
            foreach(Yii::app()->fileman->list_files($id) as $fd)
                printf("file -> [%s] [%s] [%s]\n",
                    $fd['id'],$fd['file_id'],$fd['filename']);
        }elseif($cmd=='rem'){
            // DELETE
            //
            if($fileids == ''){
                printf("usage: fileman --cmd=rem --fileids=129812,198928\n");
            }else{
                $ar = explode(",",$fileids);
                $removed = Yii::app()->fileman->remove_files($id,$ar);
                printf("file removed: %s\n",$removed);
            }
        }elseif($cmd=='path'){
            $path = Yii::app()->fileman->get_file_path($id, $fileids);
            if($path != null){
                printf("REAL FILE PATH IS: %s\n",$path);
            }
            else
                printf("file not found\d");
        }
    }
}