2

我想创建一个允许自定义下载组装的表单,就像jQuery UI 下载页面上的表单一样。用户选择她/他需要的组件,并组装、(g)压缩并发送自定义下载。这是如何运作的?我该如何写类似的东西?

可选:因为我想在 Drupal 7 站点上实现它,所以也欢迎对有用的模块提出建议。

4

3 回答 3

2

简单的实现:

<?php
    // base directory containing files that we're adding
    $dir = 'images/';

    // name of our zip file. best to use a unique name here
    $zipfile = "test.zip";

    // get a directory listing, remove self/parent directories, and reindex array
    $files = array_values(array_diff(scandir($dir), array('.', '..')));

    // form has been submitted
    if (isset($_POST['submit'])) {
        // initialize the zip file
        $output = new ZipArchive();
        $output->open($zipfile, ZIPARCHIVE::CREATE);
        // add files to archive
        foreach ($_POST['file'] as $num=>$file) {
            // make sure the files are valid
            if (is_file($dir . $file) && is_readable($dir . $file)) {
                // add it to our zip file
                $output->addFile($dir . $file);
            }
        }
        // write zip file to filesystem
        $output->close();
        // direct user's browser to the zip file
        header("Location: " . $zipfile);
        exit();
    } else {
        // display filenames with checkboxes
        echo '<form method="POST">' . PHP_EOL;
        for ($x=0; $x<count($files); $x++) {
            echo '  <input type="checkbox" name="file[' . $x . ']" value="' . $files[$x] . '">' . $files[$x] . '<br>' . PHP_EOL;
        }
        echo '  <input type="submit" name="submit" value="Submit">' . PHP_EOL;
        echo '</form>' . PHP_EOL;
    }
?>

已知错误:不$zipfile事先检查是否存在。如果是,它将被附加到。

于 2011-01-26T23:23:16.297 回答
2

jnpcl 的答案有效。但是,如果您想在不需要重定向的情况下下载文件,只需执行以下操作:

// Once you created your zip file as say $zipFile, you can output it directly
// like the following
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($zipFile));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($zipFile));
ob_clean();
flush();
readfile($zipFile);

http://php.net/manual/en/function.readfile.php

于 2011-01-26T23:35:00.567 回答
1

我对那个drupal一无所知,但可能是一些php或类似的帮助编辑器......但这可能对你有帮助...... PHP ZIP

从来没用过,但接缝不硬!

希望能帮助到你

于 2011-01-26T22:26:36.670 回答