开发者

Preparing a custom made download

I would like to create a form that will allow for custom download assembling, just like the one on jQuery UI download page. The user selects the components s/he needs and a custom download is assembled, (g)zipped and sent out. How does this work? How do I write something similar?

Optionally: since I'd like to implement this on a Drupal 7 site, suggestions for helpful 开发者_StackOverflowmodules are also welcome.


jnpcl's answer works. However, if you want to download the file without requiring a redirect, just do the following:

// 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


Simple implementation:

<?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;
    }
?>

Known bug: Doesn't check if $zipfile exists beforehand. If it does, it will be appended to.


i dont know anything about that drupal but probably is some help editor for php or similar... however this may help you... PHP ZIP

never used it but it dont seams hard!

hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜