为什么我的docx,xl​​sx,pptx文件已损坏?

时间:2014-07-03 15:38:48

标签: php ms-word docx webdav sabredav

问题:

我需要对我的服务器上的文件进行加密,它对.txt,.doc,.xls,.ppt完全正常,但不能与.docx,.xlsx和.pptx一起使用。

当我尝试编辑docx(或xlsx,pptx)时出现的问题是文件因我加密/解密的方式而被破坏,因为它不是编辑docx的正确方法。因此,当Microsoft Word尝试打开它时,它表示它已损坏并将其打开为“Document1.docx”而不是“MyFileName.docx”,并且在保存时我必须再次给出该名称并使用pptx我甚至必须给它文档所在的webdav文件夹的路径。

问题:

有没有办法让它在不需要输入路径的情况下保存在正确的位置?

代码:

以下是我用来加密文件的代码:

$ext = explode( '.', basename($path));
if (in_array("doc", $ext) || in_array("docx", $ext)) {
    $handle = fopen("$davPath/$path", "rb");
    $data_file = fread($handle, filesize("$davPath/$path"));
    fclose($handle);
} else {            
    $data_file = file_get_contents("$davPath/$path");
}

$encrypt_data_file = $encryption->encrypt($data_file);

if (file_put_contents("$davPath/encrypt_" . basename($path),$encrypt_data_file)) {
    unlink("$davPath/" . basename($path));
    rename("$davPath/encrypt_" . basename($path),"$davPath/" . basename($path));
    return true;
} else {
    return false;
}

以下是我用来解密它们的代码:

$ext = explode( '.', basename($uri));
if(is_file($davPath."/".$uri)) {
    if (in_array("doc", $ext) || in_array("docx", $ext)) {
        $handle = fopen("$davPath/$uri", "rb");
        $data_file = fread($handle, filesize("$davPath/$uri"));
        fclose($handle);
    } else {
        $data_file = file_get_contents("$davPath/$uri");
    }   
}
if ($data_file != false) {
    $decrypt_data_file = $encryption->decrypt($data_file);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($uri));
    header('Content-Location: '.$_SERVER['SCRIPT_URI']);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    echo $decrypt_data_file;
    return false;
}

PS:我确实找到了一个解决方法,其中包括在修改过程中在服务器上解密文件,但我真的不想这样做。

2 个答案:

答案 0 :(得分:1)

感谢edi9999的建议,我使用十六进制编辑器来查看未加密/解密的docx与加密/解密的docx之间的区别。

唯一的区别是在第一个结束时(没有损坏)有3次' 00'那些不在腐败之中。

没有损坏的docx的解决方案是添加3次" \ 0"到我的解密数据的末尾。现在它完美无缺!

对于docx和pptx,它有3次" \ 0"对于xlsx来说,这是4次。

答案 1 :(得分:0)

您的问题已经解决,但我想为其添加答案。

当你有一个损坏的docx时,这里有一些步骤来找出错误:

首先,尝试解压缩拉链。如果确实有效,则问题在于docx的内容。 如果解压缩不起作用,则您的zip似乎已损坏

docx

内容的问题

当您打开docx时,如果zip没有损坏,单词可能会告诉您问题所在。

它会告诉你例如:Parse error on line 213 of document.xml

这是"正常"解压缩后的docx结构。

+--docProps
|  +  app.xml
|  \  core.xml
+  res.log
+--word //this folder contains most of the files that control the content of the document
|  +  document.xml //Is the actual content of the document
|  +  endnotes.xml
|  +  fontTable.xml
|  +  footer1.xml //Containst the elements in the footer of the document
|  +  footnotes.xml
|  +--media //This folder contains all images embedded in the word
|  |  \  image1.jpeg
|  +  settings.xml
|  +  styles.xml
|  +  stylesWithEffects.xml
|  +--theme
|  |  \  theme1.xml
|  +  webSettings.xml
|  \--_rels
|     \  document.xml.rels //this document tells word where the images are situated
+  [Content_Types].xml
\--_rels
   \  .rels

docx tag wiki

所示

拉链损坏

如果zip已损坏,在大多数情况下,它们是文件开头或末尾的某些字符,不应存在(或应该存在的字符)。

最好是拥有同一文档的有效docx,并使用两个文档的十六进制表示来查看差异。

我通常使用hexdiff工具(apt-get install hexdiff)。

这通常会显示额外字符所在的位置。

通常,问题是您的标题错误。