jQuery UI ダウンロード ページのようなカスタム ダウンロード アセンブリを可能にするフォームを作成したいと思います。ユーザーが必要なコンポーネントを選択すると、カスタム ダウンロードが組み立てられ、(g)zip されて送信されます。これはどのように作動しますか?似たようなものを書くにはどうすればよいですか?
オプション: これを Drupal 7 サイトに実装したいので、役立つモジュールの提案も歓迎します。
jQuery UI ダウンロード ページのようなカスタム ダウンロード アセンブリを可能にするフォームを作成したいと思います。ユーザーが必要なコンポーネントを選択すると、カスタム ダウンロードが組み立てられ、(g)zip されて送信されます。これはどのように作動しますか?似たようなものを書くにはどうすればよいですか?
オプション: これを Drupal 7 サイトに実装したいので、役立つモジュールの提案も歓迎します。
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);
簡単な実装:
<?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
事前に存在するかどうかをチェックしません。その場合は、追加されます。
私はその drupal について何も知りませんが、おそらく php または類似のヘルプ エディターです...しかし、これはあなたを助けるかもしれません... PHP ZIP
それを使用したことはありませんが、縫い目は固くありません!
それが役に立てば幸い