1

ユーザーが必要な PDF パンフレットを選択できるフォームを作成しようとしています。ダウンロードできるように、ファイル名「Brochures.zip」として圧縮されます。

これを取得するのに問題があります。誰がどこが間違っていたかを強調できますか? ありがとう!

形 :

<label><input type="checkbox" name="brochure[]" value="file1">file1</label>
<label><input type="checkbox" name="brochure[]" value="file2">file2</label>
<label><input type="checkbox" name="brochure[]" value="file3">file3</label>
<label><input type="checkbox" name="brochure[]" value="file4">file4</label>

ダウンロード.php

$brochure = $_POST['brochure'] ;
$send = true;
if($send) {
    $zip = new ZipArchive();
    $res = $zip->open('download.zip', ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach ($brochure as $file => $val) {
            $filename = '../pdf/' . $val . '.pdf';
            $zip->addFile($filename);
        }
        $zip->close();

        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="download.zip"');
        readfile('download.zip');

    }else {
        echo 'failed';
    }
4

3 に答える 3

0

ファイルがアーカイブに追加されていない場合は、簡単なチェックを追加できます。

if (!file_exists($filename)) { die("Cannot find $filename"); }

エラー報告をアップすることもできます:

error_reporting(-1);
ini_set('display_errors', 'On');

そしてexit;直後$zip->close();。何か問題が発生した場合$zip->addFile()は返されるはずなので、警告/エラーも表示されることを願っています。false

代わりに を試しaddFromString()file_get_contents()、それが役立つかどうかを確認することもできます。私の推測では、その中の相対パスが..Zip 拡張機能をスローしていると思います。

于 2012-06-20T08:49:28.620 に答える
0

Here is the directory structure..

  • PDF

    -- file1.pdf

    -- file2.pdf

    -- file3.pdf

  • zip

    -- create.php

    -- download.php

Below is the code same as yours...

create.php

<form action="download.php" method="post">
<label><input type="checkbox" name="brochure[]" value="file1">file1</label>
<label><input type="checkbox" name="brochure[]" value="file2">file2</label>
<label><input type="checkbox" name="brochure[]" value="file3">file3</label>
<label><input type="checkbox" name="brochure[]" value="file4">file4</label>
<input type="submit">
</form>

download.php is the same as above your code. Make sure that you have right directory structure. As I said i tried your code and its working as expected creating download.zip file with selected files like file1.pdf, file2.pdf, etc...

于 2012-06-20T10:10:43.120 に答える
0

スクリプトhereを使用して問題を解決できました。また、zip ファイルを生成する前に、許可のあるフォルダーを 757 に設定する必要があることに気付きました。

于 2012-06-21T01:46:25.773 に答える