0

如何使用 PHP (fwrite) 将 Word 文档添加到另一个 Word 文档?

$filename = "./1.doc"; 
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename)); 

$filename2 = "./2.doc";
$handle2 = fopen($filename2, "r");
$contents2 = fread($handle2, filesize($filename2)); 

$contents3 =$contents2.$contents;
$fp = fopen("./3.doc", 'w+'); 

fwrite($fp, $contents); 

3.doc 仅包含 1.doc。

4

3 回答 3

3

首先,您实际上只是变量,而fwriting()不是.$contents$contents3

真正的问题是 Word 文档的内部结构更加复杂。Word 文档包含一定数量的序言和包装。如果您只是连接两个 Word 文档,您可能*只会留下一个垃圾文件。您需要一个可以解析 Word 文件、仅提取实际文本内容、连接文本并将其保存为新 Word 文件的库。

*)只是为了好玩而对其进行了测试,Word 确实无法对由两个连接的 .doc 文件组成的文件执行任何操作。

于 2009-10-19T08:46:42.550 回答
1

看起来您在最后一行的代码中有错字:

fwrite($fp, $contents);

应该

fwrite($fp, $contents3);
于 2009-10-19T08:44:57.653 回答
-1

我不会为前两个烦恼 fopen,只需 file_get_contents(),然后 fopen 3.doc 并以这种方式写入

$file1 = (is_file("./1.doc"))?file_get_contents("./1.doc"):"";
$file2 = (is_file("./2.doc"))?file_get_contents("./2.doc"):"";
$file3_cont = $file1.$file2;

if(is_file("./3.doc")){
  if(($fp = @fopen("./3.doc", "w+")) !== false){
    如果(fwrite($fp,$file3_cont)!== 假){
     echo "已写入文件 3。";
    }
  }
}
于 2009-10-19T08:45:02.790 回答