php生成文件类
php生成文件类,可以自定义文件夹,文件名称和文件格式后缀.通过php的fopen自动创建文件,而且还可以创建.htaccess文件,一般用于php的伪静态.需要注意的是,如果生成.什么文件,需要在.前加\用来转义.
<?php
/**
*php文件夹类。
*@author luhuijie
*@myblog http://www.forasp.cn/
*@copy right 2010,敬请保留,不影响速度。
*@2010-10-24
*/
class makefile{
private $file_;
private $file_name;
private $file_path;
private $file_type;
public $err_message;
function __construct($filename,$filetype,$filepath=realpath(".")){
$this->file_name = $filename;
$this->file_path = $filepath."\\";
$this->file_type = $filetype;
}
function makeit($data=""){
if(!$this->pathisexists())return false;
if($this->fileisexists())return false;
if(fopen($this->file_path.$this->file_name.$this->file_type,"w")){
if(!empty($data)){
$this->file_=fopen($this->file_path.$this->file_name.$this->file_type,"wb");
flock($this->file_,LOCK_EX);
fwrite($this->file_,$data,strlen($data));
flock($this->file_,LOCK_UN);
return true;
}
}else{
$this->seterr("没有写入权限!");
return false;
}
}
function pathisexists(){
if(file_exists($this->file_path)){
return true;
}else{
$this->seterr("不存在文件夹;");
return false;
}
}
function fileisexists(){
if(file_exists($this->file_path.$this->file_name.$this->file_type)){
$this->seterr("已经存在该文件!");
return true;
}else{
return false;
}
}
protected function seterr($massage){
$this->err_message = $massage;
}
}
$obj = new makefile("","\.htaccess",realpath("."));//注意调用的时候第1个参数为文件名,第2个是。后缀,2是路径,默认为本文件夹路径。这里的路径是E:a\b\c格式后面不需要再加\,
if(!$obj->makeit("nihao!")){echo $obj->err_message;}//如果有数据makeit(数据);
?>