1.components文件file_helper.php
2.使用
<?php
class FileHelperComponent extends Object {
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
}
/**
* create file
*/
function createFile($filename,$content = null){
$ourFileName = $filename;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $content);
fclose($ourFileHandle);
}
/**
* delete file
*/
function delFile($filename){
if (file_exists($filename) && is_file($filename)) {
unlink($filename);
}
}
/**
* file content
*/
function getContent($filename){
$content = file_get_contents($filename);
return $content;
}
/**
* base name
*/
function getBaseName($filename){
$name = basename($filename);
return $name;
}
//called after Controller::render()
function shutdown(&$controller) {
}
}
?>
2.使用
//引入
public $components = array('FileHelper');
//获得内容
$this->FileHelper->getContent($filename);
//获得基本文件名
$this->FileHelper->getBaseName($filename);
//创建文件
$this->FileHelper->createFile($filename,$content = null);
//删除文件
$this->FileHelper->delFile($filename);