1

問題 :

サーバー上のファイルを暗号化する必要があり、.txt、.doc、.xls、.ppt では問題なく機能しますが、.docx、.xlsx、および .pptx では機能しません。

docx (または xlsx、pptx) を編集しようとしたときの問題は、docx を編集する適切な方法ではないため、暗号化/復号化の方法によってファイルが破損することです。そのため、Microsoft Word がそれを開こうとすると、破損していると表示され、「MyFileName.docx」ではなく「Document1.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:変更中にサーバー上でファイルを復号化するという回避策を見つけましたが、それを行う必要はありません。

4

2 に答える 2

1

edi9999 の提案のおかげで、16 進エディターを使用して、暗号化/復号化されていない docx と暗号化/復号化された docx の違いを調べました。

唯一の違いは、最初のもの (破損していない) の最後に、破損したものには含まれていない「00」が 3 回あることです。

docx が破損しないようにするための解決策は、復号化されたデータの末尾に「\0」を 3 回追加することでした。そして今、それは完全に正常に動作します!

docx と pptx では "\0" が 3 回、xlsx では 4 回です。

于 2014-07-04T13:14:15.953 に答える
0

問題は解決しましたが、回答を追加したいと思います。

破損した docx がある場合、何が問題なのかを調べるためのいくつかの手順を次に示します。

まず、zip を解凍してみてください。それが機能する場合、問題は 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 タグ wikiに示されているとおりです。

壊れたジッパー

zip が破損している場合、ほとんどの場合、それらはファイルの先頭または末尾にあるべきではない (または存在するべきではない) 文字です。

最良の方法は、同じドキュメントの有効な docx を用意し、両方のドキュメントの 16 進数表現を使用して違いを確認することです。

私は通常、hexdiffこれにツールを使用します (apt-get install hexdiff)。

これは通常、余分な文字がどこにあるかを示します。

多くの場合、問題はヘッダーが間違っていることです。

于 2014-07-07T12:56:28.307 に答える