函数:
~~~
function cword($data, $fileName = '')
{
if (empty($data)) {
return '';
}
$data = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">' . $data . '</html>';
$dir = "./docfile/";
if (!file_exists($dir)) {
mkdir($dir, 777, true);
}
if (empty($fileName)) {
$fileName = $dir . date('His') . '.doc';
} else {
$fileName = $dir . $fileName . '.doc';
}
$writefile = fopen($fileName, 'wb') or die("创建文件失败"); //wb以二进制写入
fwrite($writefile, $data);
fclose($writefile);
return $fileName;
}
~~~
控制器:
~~~
public function editor()
{
if ($this->request->isPost()) {
$data = input('name');
cword($data, './save');
$this->success('保存word文件成功');
} else {
// 后台公共模板
$this->assign('_admin_base_layout', config('admin_base_layout'));
// 当前配色方案
$this->assign('system_color', config('system_color'));
$js = <<<JS
<script>
CKEDITOR.editorConfig = function(config) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.skin = "bootstrapck";
config.pasteFromWordRemoveFontStyles = false;
config.pasteFromWordRemoveStyles = false;
};
</script>
JS;
return ZBuilder::make('form')
->setPageTitle('新增')
->addCkeditor('name', '钩子名称', '由字母和下划线组成,如:<code>page_tips</code>')
->setExtraJs($js)
->fetch();
}
}
~~~