PHP在生成文档时在服务器上创建文档副本

时间:2012-12-04 10:39:18

标签: php mysql file download copy

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

我使用此代码从现有模板中创建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*_。感谢您的快速回复。

2 个答案:

答案 0 :(得分:3)

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;
?>

答案 1 :(得分:1)

您没有显示实际向用户发送内容的代码,但一般的想法是您应该turn on output buffering并捕获生成的文档,例如:

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

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