How to change .doc or .docx file in php

I need to modify the downloaded file .docor .docxin php. I googled, but I only found how to read it, but not the way it is. I want the word file to be as it is, and put the text at the bottom of this MS Word file. How is this possible, does anyone know, please respond.

Thank,


I used the following code: -

$w='Test';
$fp = fopen('c:/text.doc', 'a+');
fwrite($fp, $w);
fclose($fp);

It adds a line but does not appear in the word document. When I changed the doc file extension to xml , then it shows the line at the end. why it should not appear in the doc file.

Thank,

+3
source share
4 answers

Docx zip-, TbsZip, XML (Docx zip-). DOC , .

TbsZip:

<?php

$x = "Hello World!";

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the document
$zip->Open('mydoc.docx');
$content = $zip->FileRead('word/document.xml');
$p = strpos($content, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document.");

// Add the text at the end
$content = substr_replace($content, '<w:p><w:r><w:t>'.$x.'</w:t></w:r></w:p>', $p, 0);
$zip->FileReplace('word/document.xml', $content, TBSZIP_STRING);

// Save as a new file
$zip->Flush(TBSZIP_FILE, 'new.docx');
+3

DOCX XML (zipped), ...

. .

+2

, XML. MS Word, Save As → Other Formats → Choose XML .

MS Word, XML PHP DOM SimpleXML.

, w: p XML:

<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
<w:r>
<w:t>New text to be added</w:t>
</w:r>
</w:p>

W - . Word 2007+ :

http://schemas.openxmlformats.org/wordprocessingml/2006/main

XML , - MS, , .

0

I have the same task: edit the .doc or .docx file in php, for this I use this code.

Link : http://www.onlinecode.org/update-docx-file-using-php/

    $full_path = 'template.docx';
    //Copy the Template file to the Result Directory
    copy($template_file_name, $full_path);

    // add calss Zip Archive
    $zip_val = new ZipArchive;

    //Docx file is nothing but a zip file. Open this Zip File
    if($zip_val->open($full_path) == true)
    {
        // In the Open XML Wordprocessing format content is stored.
        // In the document.xml file located in the word directory.

        $key_file_name = 'word/document.xml';
        $message = $zip_val->getFromName($key_file_name);               

        $timestamp = date('d-M-Y H:i:s');

        // this data Replace the placeholders with actual values
        $message = str_replace("client_full_name",      "onlinecode org",       $message);
        $message = str_replace("client_email_address",  "ingo@onlinecode.org",  $message);
        $message = str_replace("date_today",            $timestamp,             $message);      
        $message = str_replace("client_website",        "www.onlinecode.org",   $message);      
        $message = str_replace("client_mobile_number",  "+1999999999",          $message);

        //Replace the content with the new content created above.
        $zip_val->addFromString($key_file_name, $message);
        $zip_val->close();
    }
0
source

Source: https://habr.com/ru/post/1755703/


All Articles