<?php
namespace word;
class Word
{
    function start()
    {
        ob_start();
        echo '<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">';
    }
    function save($path)
    {

        echo "</html>";
        $data = ob_get_contents();
        ob_end_clean();

        $this->wirtefile ($path,$data);
    }

    function wirtefile ($fn,$data)
    {
        $fp=fopen($fn,"wb");
        fwrite($fp,$data);
        fclose($fp);
        $this->downfile ($fn);
    }

    function downfile($fileurl)
    {
        ob_start();
        $filename=$fileurl;
        $date=date("Ymd-H:i:m");
        $size=readfile($filename);
        dump($date);
        header( "Content-type:  application/octet-stream ");
        header( "Accept-Ranges:  bytes ");
        header( "Content-Disposition:  attachment;  filename= {$date}.doc");
        header( "Accept-Length: " .$size);
    }
}

方法

    public function word()
    {

        $a=Db::table('ky_z_anonymoucontent')->where('aid',22)->select();
        $html = '';
        foreach ($a as $k=>$v )
        {
            $html.=''.$v['content'].'<hr>';
        }

//批量生成
//        for($i=1;$i<=3;$i++){
            $word = new Word();

            $word->start();
            //$html = "aaa".$i;
            $wordname = 'P淮北的个人网站--PHP10086.com'.".doc";
            echo $html;
            $word->save($wordname);
            dump($word);
            ob_flush();//每次执行前刷新缓存
            flush();

//        }

    }

标签: none

添加新评论