doc文档走的是html格式
复制以下代码直接运行即可(docAction)
public function docAction(){
$html = '<table>
<tr>
<td>头像</td>
<td><img src="http://avatar.youkuaiyun.com/1/4/1/3_qq_36025814.jpg"></td>
</tr>
<tr>
<td>姓名</td>
<td>一包伤心的辣条</td>
</tr>
<table>';
$path = APP_PATH.'/data/';#文件保存路径(绝对路径)
$fileName = '123.doc';#保存的文件名
$this->writeDoc($html, $path, $fileName);
exit();
}
public function writeDoc( $text, $path, $fileName ){
$html = '<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">'.
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
$html .= $text .'</html>';
if(!file_exists($path)){#判断文件夹是否存在
$this->createDirectory($path);
}
$fp = fopen( $path.$fileName, 'wb' );#写入试打开文件,没有则自动创建
fwrite($fp, $html);#将内容写入到文件中
fclose($fp);#函数锁定或释放文件。
}
#递归创建文件夹
function createDirectory($dir) {
return is_dir ( $dir ) or $this->createDirectory ( dirname ( $dir ) ) and mkdir ( $dir, 0777 ); //0777最高权限
}
原文:https://blog.youkuaiyun.com/qq_36025814/article/details/90203763