cfile

Commonly used functions for filesystem objects manipulation
51 followers

This extension offers commonly used functions for filesystem objects (files and directories) manipulation.

Properties

  • 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)

Methods

  • Create
  • CreateDir
  • Purge
  • Contents (get; set for files, append possible; contents filters for directories)
  • Copy
  • Rename/Move
  • Send/Download ('x-sendfile' header support)
  • Delete

Resources

Documentation

Requirements

Yii 1.0 or above

Installation

Extract the release file under protected/extensions/file

Usage

Introduce CFile to Yii. Add definition to CWebApplication config file (main.php)

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

Now you can access CFile properties and methods as follows:

$myfile = Yii::app()->file->set('files/test.txt', true);
/* 
  We use set() method to link new CFile object to our file
  First set() parameter - 'files/test.txt' - is the file path (here we supply 
  relative path wich is automatically converted into real file path such as 
  '/var/www/htdocs/files/test.txt').
  Second set() parameter - true - tells CFile to get all file properties at the 
  very beginning (it could be omitted if we don't need all of them).
  $myfile now contains CFile object, let's see what do we got there
*/
var_dump($myfile); // you may dump object to see all its properties
 
echo $myfile->size; // or get property
 
$myfile->permissions=755; // or set property
 
$mynewfile = $myfile->copy('test2.txt'); // or manipulate file somehow, eg. copy
// See CFile methods for actions available.
 
/*
Now $mynewfile contains new CFile object 
In this example file 'test2.txt' created in the same directory as our first 'test.txt' file
*/
 
/* The following is also valid */
if (Yii::app()->file->set('files/test3.txt')->exists)
    echo 'Bingo-bongo!';
else
    echo 'No-no-no.';
 
/*
Since 0.5 you can manipulate uploaded files (through CUploadedFile Yii class).
 
Let's suppose that we have the following form in our html:
 
  <form enctype="multipart/form-data" method="post">
    <input type="file" name="myupload"/>
    <input type="submit"/>
  </form>
 
After the form is submitted we can handle uploaded file as usual.
*/
$uploaded = Yii::app()->file->set('myupload');
 
// Let's copy newly uploaded file into 'files' directory with its original name.
$newfile = $uploaded->copy('files/'.$uploaded->basename);
 
/*
Since 0.6 you can use Yii path aliases.
See http://www.yiiframework.com/doc/guide/basics.namespace for information 
about path aliases.
 
Now let's get the contents of the directory where CFile resides
(supposing that it is in Yii extensions path in the 'file' subdirectory).
*/
$cfileDir = Yii::app()->file->set('ext.file');
print_r($cfileDir->contents);
 
/*
Directory contents filtering was also introduced in 0.6.
 
Futher we get all php files from $cfileDir mentioned above.
We do not need all the decendants (recursion) so we supply 'false' as the first parameter 
for getContents() method.
The second parameter describes filter, i.e. let me see only 'php' files.
You can supply an array of rules (eg. array('php', 'txt')).
NB: Moreover you can define perl regular expressions as rules.
*/
print_r($cfileDir->getContents(false, 'php'));
 
/*
Since 0.8 you can boost up file downloads.
Feature requires 'x-sendfile' header support from server (eg. Apache with mod_xsendfile or lighttpd).
If CFile::download() second parameter ('serverHandled') is set to True file download uses server internals.
*/
$myfile->download('myfastfile.txt', true);

Usage hint

The other way to use this class is to import it into Yii:

Yii::import('application.extensions.file.CFile');
 
if (CFile::set('files/test3.txt')->exists)
    echo 'Bingo-bongo!';
else
    echo 'No-no-no.';

Further reading

Detailed information about class properties and methods could be found in CFile.php source code.

Change Log

August 3, 2011

  • 0.9
    • fix: getSize() always returning formatted value (spotted by soulge)
    • fix: getSize() returning null when format set to False (spotted by iceblock)
    • fix: fail to determine mime type by extension when extension is not lowercased (closes #4)

August 20, 2010

  • 0.8
    • new: 'serverHandled' parameter in send() & download() methods allows sending [even huge] files with great speed
    • fix: be PHP 5.1+ compatible (proposed by heyhoo)
    • fix: permissions set incorrectly (spotted by heyhoo)

June 16, 2010

  • 0.7
    • new: 'flags' argument for setContents() method (proposed by TeKi)
    • fix: parameter type checks (for 'posix_getpwuid' & 'posix_getgrgid' functions) added to getOwner and getGroup methods (spotted by Spyros)

December 8, 2009

  • 0.6
    • new: set() method now supports Yii path aliases (proposed by Spyros)
    • chg: getContents() method now has 'filter' parameter to return filtered directory contents (regexp supported)
    • fix: undefined 'uploaded' variable in set() method (spotted by jerry2801)

November 22, 2009

  • 0.5
    • new: Uploaded files support (through CUploadedFile Yii class)
    • new: 'isUploaded' property
    • chg: getContents() method now has 'recursive' parameter for directories
    • fix: always recursive dirContents() method behaviour changed to appropriate

November 3, 2009

  • 0.4
    • new: 'isFile' & 'isDir' properties
    • new: rename(), move(), copy(), delete(), getSize() and getContents() methods now are able to deal with directories
    • new: purge() method to empty filesystem object
    • new: createDir() method to create directory
    • new: 'isEmpty' property
    • chg: 'formatPrecision' param of getSize() method now changed to 'format' and accepts format pattern for 'CNumberFormatter'
    • chg: download() method is now alias for primary send() method
    • chg: now 'readable' & 'writeable' properties are loaded on set() even when in non-greedy mode
    • fix: unnecessary file availability checks when 'greedy' option is specified for set() removed

October 27, 2009

  • 0.3
    • new: setBasename() method (lazy file rename)
    • new: setFilename() method (lazy file rename)
    • new: setExtension() method (lazy file rename)
    • new: download() method
    • chg: copy() & rename() methods improved (destination file name without path is enough for them to perform actions in the current file directory)
    • fix: 'extension' key existance check (in pathInfo())

October 26, 2009

  • 0.2
    • new: getContents() and setContents() methods
    • new: create() method
    • new: 'readable' & 'writeable' properties
    • fix: posix family functions existance check (in getOwner() & getGroup())

October 25, 2009

  • 0.1 Initial release.

Total 17 comments

#7619 report it
Raoul at 2012/04/03 11:14am
oups

Same here : NEKUDOTAYIM etc ... error on win platform - php 5.2.11

#7515 report it
BornToDrink at 2012/03/27 09:55am
Good Extension

But I have an issue on the Linux server (on Win localhost works), maybe it's the PHP version: Unexpected T_PAAMAYIM_NEKUDOTAYIM CFile.php lines 215 and 216. Now working with $this instead of ::

#6591 report it
jmariani at 2012/01/19 06:49pm
Thumbs up!

Thank you!

#4593 report it
got 2 doodle at 2011/07/23 12:51pm
Problem with permissions

I couldn't get this extension to create a directory with the correct permissions on my production server, not 100% sure why. Safe_mode is off.

I tried this

$imageTopFolder->createDir('0754',$path.$this->eventImageTemp);

But no joy! After much hair pulling I did this based on info from php.net and from looking at the Yii assets manager code.

$oldumask = umask(0);
@mkdir($path.$this->eventImageTemp, 0754); // or even 01777 so you get the sticky bit set
@umask($oldumask);

I don't know if others have had this problem.

#2898 report it
stutteringp0et at 2011/02/23 02:29pm
resubmitting...sorry

Sorry, that was supposed to look like this

$xsendfile=(in_array('mod_xsendfile',apache_get_modules()))?true:false;
#2897 report it
stutteringp0et at 2011/02/23 02:27pm
detecting xsendfile

Documentation (above) says: If CFile::download() second parameter ('serverHandled') is set to True...

Why not detect it within the method? [code] $xsendfile=(in_array('mod_xsendfile',apache_get_modules()))?true:false; [/code]

#2424 report it
gawronzo at 2010/12/29 04:36am
please disable one feature

Hi idle,

I have one suggestion. What do you think about disabling file size formatting while $greedy parameter of "set" function is "true" ? Now autoloaded file data contains formated size by default ...

Regards, gawronzo

#2374 report it
chux at 2010/12/18 05:12pm
Great!

Sir, you saved me a lot of time with this stuff.

Gracias!

#2001 report it
parcouss at 2010/10/28 05:56pm
Really good extension

Thank you for the work, that' really a good extension.

#1932 report it
nacesprin at 2010/10/17 10:37am
Error mb_substr_count() [<a href='function.mb-substr-count'>function.mb-substr-count</a>]: Unknown encoding &quot;63&quot;

Hello. Trying this....

$cfileDir = Yii::app()->file->set('ext.file');
    print_r($cfileDir->getContents(false,'php'));

I get follow errors:

Description
 
mb_substr_count() [<a href='function.mb-substr-count'>function.mb-substr-count</a>]: Unknown encoding &quot;63&quot;
Source File
 
C:\EasyPHP 2.0b1\www\yii-miniweb\protected\extensions\file\CFile.php(864)
 
00852:      */
00853:     private function filterPassed($str, $filter)
00854:     {
00855:         $passed = false;
00856: 
00857:         if ($filter!==null)
00858:         {
00859:             foreach ($filter as $rule)
00860:             {
00861:                 if ($rule[0]!='/')
00862:                 {
00863:                     $rule = '.'.$rule;
00864: $passed = (bool)substr_count($str, $rule, strlen($str)-strlen($rule));
00865:                 }
00866:                 else
00867:                     $passed = (bool)preg_match($rule, $str);
00868: 
00869:                 if ($passed)
00870:                     break;
00871:             }
00872:         } 
00873:         else
00874:             $passed = true;
00875: 
00876:         return $passed;
#325 report it
heyhoo at 2010/07/09 09:15am
Great

Great extension!

#642 report it
yugenekr at 2010/04/01 07:04am
great

Really usefull and handy. Thanks a lot.

#688 report it
Koneko13 at 2010/03/18 08:31am
Must have extension!

Really useful.

#1104 report it
Spyros at 2009/12/08 03:34am
That's Great

Very usefull, saved me a lot of time :)

#1105 report it
idle at 2009/12/07 11:56pm
Re: is this free?

@jackiesun: As free as in beer, why would you think it is not? :)

#1106 report it
jackiesun at 2009/12/07 09:18pm
is this free?

is this free?

#1157 report it
jerry2801 at 2009/11/20 09:33am
is useful!

Thank you!

Leave a comment

Please to leave your comment.

Create extension
  • License: MIT License
  • Developed by: idle
  • Category: File System
  • Votes: +45
  • Downloaded: 4,087 times
  • Created on: Oct 25, 2009
  • Last updated: Aug 3, 2011