如何在php中修改.doc或.docx文件
我必须在如何在php中修改.doc或.docx文件,php,tabs,Php,Tabs,我必须在php中修改上传的.doc或.docx文件。我在谷歌上搜索了一下,但我只找到了如何阅读它,而不是它的本来面目。 我希望word文件保持原样,并将文本放在MS word文件的底部。 这怎么可能,任何人都知道,请回复 谢谢 我使用了以下代码:- $w='Test'; $fp = fopen('c:/text.doc', 'a+'); fwrite($fp, $w); fclose($fp); 它附加了字符串,但未显示在word文档中。 当我将doc文件的扩展名更改为xml时,它会在末尾显
php
中修改上传的.doc
或.docx
文件。我在谷歌上搜索了一下,但我只找到了如何阅读它,而不是它的本来面目。
我希望word文件保持原样,并将文本放在MS word文件的底部。
这怎么可能,任何人都知道,请回复
谢谢
我使用了以下代码:-
$w='Test';
$fp = fopen('c:/text.doc', 'a+');
fwrite($fp, $w);
fclose($fp);
它附加了字符串,但未显示在word文档中。
当我将doc文件的扩展名更改为xml时,它会在末尾显示字符串。
为什么不应该显示在文档文件中
谢谢,DOCX格式的文档应该是XML(压缩),因此您可以尝试解析和修改它们
有关格式的详细信息,请参阅。我建议将文件保存为XML。使用MS Word编写文档时,请转到另存为->其他格式->在此处选择XML 您仍然可以用MS Word打开文档,并且使用PHPDOM或SimpleXML编辑XML也很容易 要在底部添加一些文本,只需在XML正文中添加一个新的w:p元素:
<w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
<w:r>
<w:t>New text to be added</w:t>
</w:r>
</w:p>
在较旧的XML格式中,有不同的名称空间,因此您必须查看MS网页,以确定哪一个是正确的。对于Docx文件,您可以轻松使用zip读取器,例如,以便读取和编辑主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');
我有同样的任务要在php中编辑.doc或.docx文件,我已经使用了这段代码
参考:
谢谢回复,实际上我正在上传.doc文件,并在文件末尾插入一些数据。然后将.doc文件的url插入数据库。然后当我需要.doc文件时,我会下载它。如果我把它转换成xml,它在word文档中是如何变化的。word文档是xml。至少来自Word2007。甚至2003年也已经是某种XML,但可能是无效的。
<?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');
$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();
}