This extension offers commonly used functions for filesystem objects (files and directories) manipulation.
Yii 1.0 or above
Extract the release file under protected/extensions/file
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);
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.';
Detailed information about class properties and methods could be found in CFile.php source code.
Total 17 comments
Same here : NEKUDOTAYIM etc ... error on win platform - php 5.2.11
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 ::
Thank you!
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.
I don't know if others have had this problem.
Sorry, that was supposed to look like this
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]
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
Sir, you saved me a lot of time with this stuff.
Gracias!
Thank you for the work, that' really a good extension.
Hello. Trying this....
I get follow errors:
Great extension!
Really usefull and handy. Thanks a lot.
Really useful.
Very usefull, saved me a lot of time :)
@jackiesun: As free as in beer, why would you think it is not? :)
is this free?
Thank you!
Leave a comment
Please login to leave your comment.