[EXTENSION] CFile

Version 0.5 is available, introducing uploaded files handling (see CFile Documentation for usage example).

  • 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

http://www.yiiframework.com/extension/cfile/

[size="7"]more useful example[/size]

from view:


JHtml::activeFileField(new Node, 'photo');

in controller can be fetch by set:


$photoFile = Yii::app()->file->set('Node[photo]');

i sumit a ticket for a bug~

http://code.google.com/p/ist-yii-cfile/issues/detail?id=1

Yeap, and it’s already fixed in SVN.

suggest adding a "force_delete" parameter in delete() method.

If file or folder is not writable, will use chmod() before unlink() and rmdir().

I would suggest that set should work with alias too

eg




$myExportsDir = Yii::app()->file->set("application.exports")



Also

$file->getContents($filter)

where filter can be a string ".jpg" or an array(".jpg",".gif",".png")

could be useful

Interesting option though arguable. Mainly from the security point of view. I think potential risk to remove everything under / is spooky. Could you tell me whether your http server uses root priviledges?

How do we benefit from that, Spyros?

That’s the sugar I was thinking to introduce in 0.6 :) Thank you for your interest.

It’s easier to write application.exports than Yii::app()->basePath.DIRECTORY_SEPARATOR.“exports” :)

Also there’s the webroot alias that points outside the protected folder

Another thing I was wondering. Is there a reason that there’s no timeCreated attribute?

But how would you know, that it’s a path alias? Maybe use a second parameter to indicate that it’s an alias (defaulting to false)? Apart from that, i also think path aliases should be supported. They are very useful to make component filepaths configurable and also allow easy access to files inside a module.

It’s easy to implement: http://www.yiiframework.com/doc/guide/basics.namespace

If you don’t have permission to delete a file, you’re also not permitted to chmod the file. So that wouldn’t be very reliable anyway.

You first check if Yii::getPathOfAlias($alias) returns an existing file if not you treat it as url

Hmm, but that’s “expensive”, don’t you think so? I see, that set() already has a second parameter. So how about simply separating it into a setFromAlias()?

File creation time you may be accustomed to have in Windows is a very ambiguous thing, essentially there is no way to get that timestamp on Unix, as it is considered useless. I don’t know whether we need filectime() ambiguousness here in CFile.

Seems to be so. I’ll think about possible set() overloading solution.

Version 0.6 is available, introducing Yii aliases handling and directory contents filters (see CFile Documentation for usage examples).

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

http://www.yiiframework.com/extension/cfile/

It would be great to see some transaction capability, so that file operations can be completed atomically.

What exactly do you mean, any examples?

Hello.

I’m starting to use your extension, it is very helpful, thank you.

I have a set of image files that start with the same prefix and I want to apply a flter to avoid having to process all the files.




Array

(

    [0] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/.DS_Store

    [1] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213014520_68949.jpg

    [2] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213014520_74450.jpg

    [3] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213014520_87550.jpg

    [4] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213015037_33518.jpg

    [5] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213015037_41118.jpg

    [6] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213015037_59718.jpg

    [7] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213015037_70319.jpg

    [8] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213024408_14856.jpg

    [9] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213024408_86055.jpg

    [10] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213024408_95855.jpg

    [11] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213030709_91220.jpg

    [12] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213030709_96520.jpg

    [13] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213030709_99220.jpg

    [14] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213230123_84355.jpg

    [15] => /Applications/xampp/xamppfiles/htdocs/vmc_yii/protected/tmp/20100213230536_42602.jpg

)



And I want to filter those that start with: 20100213014520_

The extension could be JPG, GIF or PNG.

I am not good with regex, could you please help me how to use a regex to filter these files?

Thank you.

Pattern describing files starting with ‘20100213014520_’ looks like ‘~/20100213014520_.*/~’. Nb: this should eventually return files regardless of their extension.

On regular expressions: http://www.regular-expressions.info/quickstart.html

A small utility to play with regex: http://weitz.de/regex-coach.

Thank you very much! I really appreciate your help.

Developers such as yourself are a big reason why I stay with YIi Framework.

I mean the same as a database transaction.




<?php

try

{

  $file = Yii::app()->file;

  $transaction = $file->beginTransaction();

  $file->set('file1');

  $file->rename('other/file1');

  $file->set('file2');

  $file->delete();

  $file->set('file3');

  $file->rename('other/file3'); // will fail for some reason! throw CException

  $transaction->commit(); // commits file operations

}

catch(Exception $e)

{

  $transaction->rollback(); // undoes file operations

}