龚哥哥 爱生活、做自己!
PHP文件生成模块
发表于 2015-9-1 | PHP
<?php
 /*
 *    描    述    :    把数据写入文件
 */
 namespace Model\Backend;
 class FilePutModel
 {
    /* 关闭自动连接数据库 */
    protected $autoCheckFields = false;
    private $m_DirName;  //目录地址
    private $m_FileName;  //文件名称
    private $m_Suffis;  //文件后缀名
    private $m_DataArray;  //需要写入文件的数据
    /*
     *    构造方法
     */
    public function __construct($DirName, $FileName, $DataArray, $Suffis = null)
    {
        $this->m_DirName = null;
        $this->m_FileName = null;
        $this->m_FileSuffix = null;
        $this->m_DataArray = array();
        /* 基础数据设置 */
        $this->m_DirName = $DirName;
        $this->m_FileName = $FileName;
        $this->m_FileSuffix = $Suffis;
        $this->m_DataArray = $DataArray;
        
        /* 基础数据处理 */
        $this->IsDirThere();
        $this->IsFileSuffix();
    }
    /*
     *    数据处理
     */
    public function SetFileData()
    {
        if(false == empty($this->m_FileName) && false == empty($this->m_DataArray)) {
            $Statu = file_put_contents(PATH_PHP_FILE.$this->m_FileName.'.'.$this->m_FileSuffix, "<?php\n\rreturn ".var_export($this->m_DataArray, true).";\n\r?>");
            if(false != $Statu) {
                return true;
            } else {
                return false;
            }
        } else {
            return '数据不能为空!';
        }
    }
    
    /*
     *    判断目录是否存在
     */
    private function IsDirThere()
    {
        if(false == is_dir($this->m_DirName)) {
            mkdir($this->m_DirName, 0777, true);
        }
    }
    
    /*
     *    判断后缀名是否存在
     */
    private function IsFileSuffix()
    {
        if(true == empty($this->m_FileSuffix)) {
            $this->m_FileSuffix = 'php';
        }
    }
}
 ?>
//使用说明
$FilePutObj = new \Model\Backend\FilePutModel('路径',文件名称,'数据','文件后缀名');
        if(true == $FilePutObj->SetFileData()) {
            //成功
        }

发表评论:

TOP