【问题标题】:PHP Create document copy on server when a document is generatedPHP 生成文档时在服务器上创建文档副本
【发布时间】:2012-12-04 10:39:17
【问题描述】:

在我的应用程序中,用户可以根据租金创建收据。

我使用此代码从现有模板创建 Word 文件 (.doc)。

        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=Rent_Receipt_". $_SESSION['custid']    .".doc");
        include ("templates/template_receipt.php");

现在用户将把它存储在他的本地计算机上,但是我怎样才能让服务器将这个相同的文档存储在服务器文件夹中,这样用户就不必手动上传了?

提前致谢。

编辑
include: template_receipt.php 包含要创建的 Word 文档的 HTML 代码:

<?php echo "
<html xmlns:v='urn:schemas-microsoft-com:vml'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
xmlns:m='http://schemas.microsoft.com/office/2004/12/omml'
xmlns='http://www.w3.org/TR/REC-html40'>

<head>
<meta http-equiv=Content-Type content='text/html; charset=windows-1252'>
<meta name=ProgId content=Word.Document>
<meta name=Generator content='Microsoft Word 14'>
<meta name=Originator content='Microsoft Word 14'>
<link rel=File-List href='Template%20Verhuurbon_files/filelist.xml'>
<link rel=Edit-Time-Data href='Template%20Verhuurbon_files/editdata.mso'>
<link rel=dataStoreItem href='Template%20Verhuurbon_files/item0001.xml'
target='Template%20Verhuurbon_files/props002.xml'>
<link rel=themeData href='Template%20Verhuurbon_files/themedata.thmx'>
<link rel=colorSchemeMapping
href='Template%20Verhuurbon_files/colorschememapping.xml'>

等等。稍后我将研究这个ob*_。感谢您的快速响应。

【问题讨论】:

    标签: php mysql file download copy


    【解决方案1】:

    ob_* 函数会有所帮助。
    例如:

    <?php
        // geting file content
        ob_start();
        include ("templates/template_receipt.php");
        $content = ob_get_contents();
        ob_end_clean();
    
        //store in local file
        file_put_contents('/file/name.txt',$content);
    
        // file output:
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=Rent_Receipt_". $_SESSION['custid']    .".doc");
        echo $content;
    ?>
    

    【讨论】:

      【解决方案2】:

      您没有显示实际将内容发送给用户的代码,但总体思路是您应该turn on output buffering 并捕获生成的文档,例如:

      ob_start();
      include ("templates/template_receipt.php");
      $document = ob_get_clean();
      

      完成此操作后,您可以使用简单的echo $document 将文档发送给用户,同时使用file_put_contents('local_copy', $document) 之类的内容将其保存在本地。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 2011-04-20
        • 2019-10-07
        • 1970-01-01
        相关资源
        最近更新 更多