问题:
我需要在我的服务器上的文件进行加密,它的工作非常好。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:我确实找到了一个解决方案,其中包括在修改期间在服务器上解密文件,但我真的不想这样做。
感谢edi9999的建议,我使用十六进制编辑器来查看未加密/解密的docx和加密/解密的docx之间的差异。
唯一的区别是在第一个(未损坏)的末尾有3个'00'不在损坏的那个中。
没有损坏的docx的解决方案是在我的解密数据的末尾添加3倍的"'0"。现在它工作得很好!
对于docx和pptx它是3倍的"'0"对于xlsx它是4倍的
你的问题已经解决了,但我想补充一个答案。
当您有一个损坏的docx时,这里有一些步骤来找出问题所在:
首先,尝试解压缩zip文件。如果它确实有效,那么问题出在docx的内容上。如果解压缩不工作,您的zip文件似乎已损坏
docx内容的问题
当您打开docx文件时,如果zip文件没有损坏,word可能会告诉您问题出在哪里。
它会告诉你例如: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标签wiki所示。
损坏zip
如果zip文件被损坏,在大多数情况下,它们是文件开头或结尾的一些字符,这些字符不应该在那里(或者应该在那里但没有)。
最好是拥有同一文档的有效docx,并使用两个文档的十六进制表示来查看差异。
我通常使用hexdiff
工具(apt-get install hexdiff)。
这通常会显示额外字符的位置。
通常,问题是你有错误的标题