Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Create document copy on server when a document is generated

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.

like image 469
TMNuclear Avatar asked Dec 30 '25 03:12

TMNuclear


1 Answers

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;
?>
like image 142
StasGrin Avatar answered Dec 31 '25 18:12

StasGrin