Yii 1.1: cfile

Commonly used functions for filesystem objects manipulation

This extension offers commonly used functions for filesystem objects (files and directories) manipulation. Can be used separately from Yii.

Please use CFile project repository at GitHub for your reports.

Documentation is available at CFile repository.

CFile exposes:


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


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

Total 20 comments

#17914 report it
paskuale at 2014/08/11 06:06am
delete file before send it

Hi I've a function for send file to browser, but if I check $file-exists() and delete it before, don't send any file !! Why ?

#16424 report it
Trejder at 2014/02/21 09:35am
Downloading large files with CFile extension

CFile offers you support of mod-xsendfile plugin, if you're using Apache server and have it properly configured to use it. If not, it falls back to old, bad readfile. This core PHP function will fail on files even as small as 100-200 MB, even if you have quite very high maximum memory limit set.

What is the saddest, you may not even notice, that your script (that uses CFile, that uses readfile) fails, because -- due to some stupidity of PHP creators -- on certain server configurations, your file will be downloaded in this case, but instead of real content, it will contains a PHP generated HTML code with "exhausted memory" error message.

To avoid this problem, if you can't use mod-xsendfile you have to manually extend CFile to use file chunking. Here you have my answer on Stack Overflow. Use provided code in place, where CFile extension calls readfile function.

#16421 report it
Trejder at 2014/02/21 07:54am
@Kreshnik Arapi

Take a look at comment #4593 below, by got 2 doodle, made at 2011/07/23 12:51pm. You'll find some examples on CFile::createDir there.

#16129 report it
Kreshnik Arapi at 2014/01/22 04:04am

Hello! I am a little bit new with Yii and I don't know how can to create a new directory and how to view its contents. May somebody help me please! Thank you very much!

#14780 report it
Trejder at 2013/09/09 06:56am
Use `readfile` instead of `echo $contents`

Consider changing CFile to use readfile instead of echo $contents, which is more secure in case of binary files. I found a notice like that somewhere in PHP's readfile() function description and changed your extension manually to fit this.

#13738 report it
Thureos at 2013/06/20 01:32pm
setContents Error!


First of all, thanks for this class...

I've found an error in setContents method:

If I set autocreate to true, it never creates the file because it validates if is a file before and that will never happen.

Sorry for my english!

Thanks again!

#12982 report it
Trejder at 2013/04/25 04:29am

Support for FTP and other protocols is native in PHP at most file-operating functions. See "Supported Protocols and Wrappers". This means, that you should be able to simply use any of supported protocols (for example file://) when specifing file name via CFile->filename.

#12981 report it
Trejder at 2013/04/25 04:21am
Base class

CFile is currently based on CApplicationComponent. Consider, if you could base it on CFileHelper. This way users of your extension can have access to functions offered by both file-related classes.

#11806 report it
roadrunner at 2013/02/05 02:35am
it copy very slow

Hi when i use cfile it takes time to copy and sometime error shows Maximum execution time of 30 seconds, how can i speed up copy? also it needs to refresh if loading is stop to continue the copy. and how can i put an ajax their or i want that something loading .gif is show while it still copying????

on the other hand, it cool extension.....

#8728 report it
Vitalets at 2012/06/22 03:41am

good extension! It would be cool to add FTP support as all used php functions support it now.

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

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


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

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

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

Sorry, that was supposed to look like this

#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

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


#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');

I get follow errors:

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;
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);
00869:                 if ($passed)
00870:                     break;
00871:             }
00872:         } 
00873:         else
00874:             $passed = true;
00876:         return $passed;

Leave a comment

Please to leave your comment.

Create extension