In the previous article,i have been posted about how to merger pdf file using pdfmerger, in this session i will sharing   how to merger file docx using php,especially using tbszip php library. So, before we start create program, we must download tbszip library here.

The next step is create a php file, example mergerdocx.php. After that copy code this code to the mergerdocx.php

include_once('tbszip.php');
$zip = new clsTbsZip();
// Open the first document
$zip->Open('MM1.3.docx');
$content1 = $zip->FileRead('word/document.xml');
$zip->Close();
// Extract the content of the first document
$p = strpos($content1, 'if ($p===false) exit("Tag not found in document 1.");
$p = strpos($content1, '>', $p);
$content1 = substr($content1, $p+1);
$p = strpos($content1, '
');
if ($p===false) exit("Tag
not found in document 1.");
$content1 = substr($content1, 0, $p);
// Insert into the second document
$zip->Open('MM 1.6 (rev).docx');
$content2 = $zip->FileRead('word/document.xml');
$p = strpos($content2, '
');
if ($p===false) exit("Tag not found in document 2.");
$content2 = substr_replace($content2, $content1, $p, 0);
$zip->FileReplace('word/document.xml', $content2, TBSZIP_STRING);
// Save the merge into a third file
$zip->Flush(TBSZIP_FILE, 'merge.docx');
?>

I try to merger doc file using the above code, but not success, so the above code just work to merger docx file.

Furthermore, the above code can not display image, so, if in docx file there is a image, after merger it, the image can not display.

If you have a way to merger doc or docx file using php, i hope you can share in this blog.
Title: How to merger file docx using php script
Posted by faisal

Thanks for reading about How to merger file docx using php script