Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 准备定制下载_Php_Javascript_Download_Drupal 7 - Fatal编程技术网

Php 准备定制下载

Php 准备定制下载,php,javascript,download,drupal-7,Php,Javascript,Download,Drupal 7,我想创建一个允许自定义下载组装的表单,就像上面的表单一样。用户选择所需的组件,然后组装、打包并发送自定义下载。这是怎么回事?我怎么写类似的东西 可选:由于我希望在Drupal 7站点上实现此功能,因此也欢迎提供有用模块的建议。我对Drupal一无所知,但可能是php或类似的帮助编辑器。。。然而,这可能会帮助你 从来没用过,但它不难缝 希望它有助于简单的实现: <?php // base directory containing files that we're adding

我想创建一个允许自定义下载组装的表单,就像上面的表单一样。用户选择所需的组件,然后组装、打包并发送自定义下载。这是怎么回事?我怎么写类似的东西


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

我对Drupal一无所知,但可能是php或类似的帮助编辑器。。。然而,这可能会帮助你

从来没用过,但它不难缝

希望它有助于

简单的实现:

<?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是否存在。如果有,它将被附加到。

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);

+1,虽然这只涵盖了最明显的部分,一个我自己可以弄明白的部分。让我头疼的是我如何让表单提交按钮下载压缩包。你让表单向php文件提交复选框值,在php中检查文件1的复选框1是否被选中,然后将其添加到zip。。。然后对于复选框2等等,当它结束时,关闭zip文件并将其放入某个文件夹,只需将用户重定向到该文件标题位置:newfiles/thatfileyoumake.zip;像这样的事。。。帮助?谢谢,我相信这正是我想要的,以便将文件发送过来。我以前听说过一些关于发送适当的头的事情,但我以前从来没有这样做过:。