Thursday, May 29, 2014

Yii: Step by Step Working with CFile Extension

CFile extension is commonly used for filesystem objects (files or directories) manipulate. The following step will show you how to config till how to use it.

Setup
1. Click on -> CFILE and download the folder.
2. Unzip and copy the folder in to protected/extensions path
3. Rename the folder to file
4. Add the following code into protected/config/main.php

'components'=>array(
    'file' => array(
        'class' => 'application.extensions.file.CFile',
     ),
 ),

How to Use
// yourfile
$yourfile = 'sample.txt';

// Call extension into $cfile variable
$cfile = Yii::app()->file;

// Check file exist
$cfile->set($yourfile)->exists;

// Create file
$cfile->set($yourfile)->create();

// Append Content
$cfile->set($yourfile)->setContents('treat me a coffee for this tutorial!');

// Get Content from file
$cfile->set($yourfile)->getContents();

// Delete File
$cfile->set($yourfile)->delete();


Others available method as follow:
  • Create
  • CreateDir
  • Purge
  • Contents (get; set for files, append possible; contents filters for directories)
  • Copy
  • Rename/Move
  • Send/Download ('x-sendfile' header support)
  • Delete
The properties of file you can check as follow:
  • exists
  • isdir
  • isfile
  • isempty
  • isuploaded
  • readable
  • writeable
  • realpath
  • basename (setter available)
  • filename (setter available)
  • dirname
  • extension (setter available)
  • mimeType
  • timeModified
  • size
  • owner (setter available)
  • group (setter available)
  • permissions (setter available)

No comments:

Post a Comment