无法使用PHP ZipArchive更新word/rels/document.xml.rels
我正在尝试使用PHP将二进制数据作为图像加载到Word文档(Opem XML)中,以便以后与XSLT一起使用 在以PHP ZipArchive打开Word文档后,我能够成功地将图像加载到Word/media文件夹中,并更新Word/document.xml文件。但是我无法更新word/rels/document.xml.rels文件中的无法使用PHP ZipArchive更新word/rels/document.xml.rels,php,openxml,ziparchive,Php,Openxml,Ziparchive,我正在尝试使用PHP将二进制数据作为图像加载到Word文档(Opem XML)中,以便以后与XSLT一起使用 在以PHP ZipArchive打开Word文档后,我能够成功地将图像加载到Word/media文件夹中,并更新Word/document.xml文件。但是我无法更新word/rels/document.xml.rels文件中的 我已经交叉检查了xml的格式是否正确 下面是我尝试使用的代码片段 $zipArchive=new ZipArchive(); $zipA
我已经交叉检查了xml的格式是否正确
下面是我尝试使用的代码片段
$zipArchive=new ZipArchive();
$zipArchive->open($pathToDoc);
$imagePre="image";
$relIdPre="rId";
$index=100;
$nodeList = $reportDOM->getElementsByTagName("Node");
$i=0;
foreach($nodeList as $node) {
$divList = $node->getElementsByTagName("*");
foreach ($divList as $divNode) {
if (strncasecmp($divNode->nodeName, "wizChart", 8) == 0) {
$imgData=$divNode->getAttribute("src");
$imgData=base64_decode(substr($imgData,22));
$zipArchive->
addFromString("word/media/".$imagePre."".$index.".png",$imgData);
$fp=$zipArchive->getStream("word/_rels/document.xml.rels");
$contents='';
while (!feof($fp)) {
$contents .= fread($fp, 2);
}
$serviceOutput=new DOMDocument();
$serviceOutput->loadXML($contents);
$serviceList=$serviceOutput->getElementsByTagName("Relationships");
$element=$serviceOutput->createElement("Relationship");
$element->setAttribute("Id",$relIdPre."".$index);
$element->setAttribute("Type","http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
$element->setAttribute("Target","word/media/".$imagePre."".$index.".png");
foreach ($serviceList as $serviceNode) {
$serviceNode->appendChild($element);
}
$zipArchive->addEmptyDir("word/_rels/");
$zipArchive->addFromString("word/_rels/document.xml.rels", $serviceOutput->saveXML());
$index++;
}
}
}
$zipArchive->close();
有人能建议我可能做错了什么吗?添加PNG时,您也在添加新的内容类型,因此需要在[content\u Types].xml中进行设置。有关更多详细信息,请参阅。您没有确切说明发生了什么(或没有发生什么)。文件是否未更改?如果我注释掉试图将xml节点添加到docmuents.xml.rels文件的部分,图像将保存到word文档中。但是,如果我尝试将这些关系添加到.rels文件中,即使图像也不会保存。我正在使用下面链接中提到的PHP代码,并尝试在将$newContent应用于word/document.xml之前将图像添加到$outputDocument。谢谢您的帖子,但是[content Types].xml中已经有PNG内容类型。正如我之前所说,我能够将图像添加到word文档中。我无法将关系添加到_rels/document.xml.rels文件。该代码不仅在我尝试添加关系时有效,如果我注释掉该部分,我就能够将图像添加到文档中。