PHP Create document copy on server when a document is generated

a 夏天 提交于 2019-12-12 17:07:05

问题


In my application, the user can create a receipt according to the rent items.

I use this code to create a Word file (.doc) out of an exisiting template.

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

Now the user will have this stored on his local computer, but how can I make the server to store this same document in the server folder, so the user don't have to upload it manually?

Thanks in advance.

EDIT
The include: template_receipt.php contains HTML code for the Word document to be created:

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

And so on. I'm going to look into this ob*_ in a second. Thank you for fast response.


回答1:


ob_* functions will help.
For example:

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


You are not showing the code that actually sends the content to the user, but the general idea is that you should turn on output buffering and capture the generated document, e.g:

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

After doing this you can send the document to the user with a simple echo $document and at the same time save it locally with something like file_put_contents('local_copy', $document).



来源:https://stackoverflow.com/questions/13701029/php-create-document-copy-on-server-when-a-document-is-generated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!