【问题标题】:Preparing a custom made download准备自定义下载
【发布时间】:2011-01-26 22:13:00
【问题描述】:

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

可选:由于我想在 Drupal 7 站点上实现此功能,因此也欢迎提供有用模块的建议。

【问题讨论】:

    标签: php javascript download drupal-7


    【解决方案1】:

    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

    【讨论】:

    • 谢谢,我相信这正是我想要发送文件的内容。我之前听说过发送适当的标头,但我以前从未真正这样做过:)。
    【解决方案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 是否存在。如果是,它将被附加到。

    【讨论】:

      【解决方案3】:

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

      没用过,但是不硬!

      希望对你有帮助

      【讨论】:

      • +1,尽管这仅涵盖最明显的部分,我自己可以弄清楚。让我烦恼的是我如何让表单提交按钮下载压缩包。
      • 您制作表单,将复选框值提交到 php 文件,并在 php 中检查文件 1 的复选框 1 是否已检查,然后将其添加到 zip 中...然后复选框 2 等等一旦结束,您关闭 zip 文件并将其放入某个文件夹,然后将用户重定向到该文件头(“位置:newfiles/thatfileyoumade.zip”);类似的东西...有帮助吗?
      猜你喜欢
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多