idlesign/ist-yii-cfile

CFile提供从Yii和独立环境中操作文件系统对象的方法

v1.0.0 2013-07-05 16:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:37:04 UTC


README

http://github.com/idlesign/ist-yii-cfile

警告

该项目不再维护。感谢关注。

这是什么

ist-yii-cfile是Yii框架的一个扩展,包含用于文件系统对象(文件和目录)操作的一些常用函数。

该扩展也可以以独立模式运行,即不使用Yii。

快速概述

  • 属性
    • exists
    • isdir
    • isfile
    • isempty
    • isuploaded
    • readable
    • writeable
    • realpath
    • relativepath
    • basename (+setter)
    • filename (+setter)
    • dirname
    • extension (+setter)
    • mimeType
    • timeModified
    • size
    • owner (+setter)
    • group (+setter)
    • permissions (+setter)
  • 方法
    • create
    • createdir
    • purge
    • contents
    • copy
    • rename/move
    • send/download
    • delete

要求

  • 使用作为Yii扩展时,需要PHP 5.1+和Yii 1.0或更高版本。
  • 不使用Yii时,需要PHP 5.1+。

安装

  • 对于Yii:将扩展文件解压到protected/extensions/file目录下。
  • 不使用Yii:将扩展文件解压到选择的目录。

使用

与Yii框架一起使用

  • 向Yii引入CFile。
  • 在CWebApplication配置文件(main.php)中添加定义
'components'=>array(
    ...
    'file'=>array(
        'class'=>'application.extensions.file.CFile',
    ),
    ...
),
  • 现在您可以如下访问CFile属性和方法
$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, e.g. copy.

// Please 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 https://yiiframework.cn/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);
  • 使用该类的另一种方法是将其导入Yii
Yii::import('application.extensions.file.CFile');

if (CFile::set('files/test3.txt')->exists) {
    echo 'Bingo-bongo!';
} else {
    echo 'No-no-no.';
}

不使用Yii使用

需要时简单地导入CFileHelper.php,并使用CFileHelper::get()获取文件系统资源的CFile对象。

$cf_file = CFileHelper::get('files/test.txt');  // $cf_cile now contains CFile object, use it as required.
$cf_file->copy('mycopy.txt');

进一步阅读

有关类属性和方法的详细信息,可以在CFile.php源代码中找到,请尽情挖掘。