PHP 表单 - 下载到 zip

发布于 2024-08-16 23:02:04 字数 676 浏览 8 评论 0原文

我有以下形式:

<form action="download.php" method="get">
<input type="checkbox" name="file1" /> File1 <br/>
<input type="checkbox" name="file2" /> File2 <br/>
<input type="checkbox" name="file3" /> File3 <br/>
<input type="checkbox" name="file4" /> File4 <br/>
<input type="checkbox" name="file5" /> File5 <br/>

<input type="submit" name="mysubmit" value="Download!">   
</form> 

然后我不能获取勾选值:

<?php echo $_GET["file1"]; ?>

给出结果: on

但是我想要做的是选择这些选项,每个选项都与一个 PHP 文件相关,on提交的每个文件都被合并成一个 ZIP

任何帮助表示赞赏。

I have the following form:

<form action="download.php" method="get">
<input type="checkbox" name="file1" /> File1 <br/>
<input type="checkbox" name="file2" /> File2 <br/>
<input type="checkbox" name="file3" /> File3 <br/>
<input type="checkbox" name="file4" /> File4 <br/>
<input type="checkbox" name="file5" /> File5 <br/>

<input type="submit" name="mysubmit" value="Download!">   
</form> 

I cant then GET the ticked value:

<?php echo $_GET["file1"]; ?>

Gives the result: on

However want I want to be able to do is select those options, and each option relates to a PHP file, on Submit each file is combiled into a ZIP

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当梦初醒 2024-08-23 23:02:04

首先,向表单字段添加一个值字段并将其更改为数组:

<form action="download.php" method="get">
<input type="checkbox" name="file[0]" value="1" /> File1 <br/>
<input type="checkbox" name="file[1]" value="1" /> File2 <br/>
<input type="checkbox" name="file[2]" value="1" /> File3 <br/>
<input type="checkbox" name="file[3]" value="1" /> File4 <br/>
<input type="checkbox" name="file[4]" value="1" /> File5 <br/>
<input type="submit" name="mysubmit" value="Download!">   
</form> 

接下来,在 download.php 中:

if (!empty($_POST['file'])) {
    // open zip
    $zip_path = '/path/to/created/download.zip';
    $zip = new ZipArchive(); 
    if ($zip->open($zip_path, ZIPARCHIVE::CREATE) !== TRUE) {
        die ("An error occurred creating your ZIP file.");
    }  
    // checkbox values dont matter because only checked boxes show up in POST data
    foreach ($_POST['file'] as $key => $val) {
        // generate filename to add to zip
        $filename = '/path/to/php/file' . $key . '.php';
        $zip->addFile($filename) or die ("ERROR: Could not add the file $filename");  
    }
    $zip->close();

    //===============
    // force download
    //===============
// assume you have a full path to file stored in $zip_path
if (!is_file($zip_path)) {
  die('The file appears to be invalid.');
}

$zip_path = str_replace('\\', '/', realpath($zip_path));
$filesize = filesize($zip_path);
$filename = substr(strrchr('/'.$zip_path, '/'), 1);
$extension = strtolower(substr(strrchr($zip_path, '.'), 1));

// use this unless you want to find the mime type based on extension
$mime = array('application/octet-stream');

header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE only headers
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))) {
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
} else {
  header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);


} // close $_POST check

First, add a value field to your form fields and change them to an array:

<form action="download.php" method="get">
<input type="checkbox" name="file[0]" value="1" /> File1 <br/>
<input type="checkbox" name="file[1]" value="1" /> File2 <br/>
<input type="checkbox" name="file[2]" value="1" /> File3 <br/>
<input type="checkbox" name="file[3]" value="1" /> File4 <br/>
<input type="checkbox" name="file[4]" value="1" /> File5 <br/>
<input type="submit" name="mysubmit" value="Download!">   
</form> 

Next, in download.php:

if (!empty($_POST['file'])) {
    // open zip
    $zip_path = '/path/to/created/download.zip';
    $zip = new ZipArchive(); 
    if ($zip->open($zip_path, ZIPARCHIVE::CREATE) !== TRUE) {
        die ("An error occurred creating your ZIP file.");
    }  
    // checkbox values dont matter because only checked boxes show up in POST data
    foreach ($_POST['file'] as $key => $val) {
        // generate filename to add to zip
        $filename = '/path/to/php/file' . $key . '.php';
        $zip->addFile($filename) or die ("ERROR: Could not add the file $filename");  
    }
    $zip->close();

    //===============
    // force download
    //===============
// assume you have a full path to file stored in $zip_path
if (!is_file($zip_path)) {
  die('The file appears to be invalid.');
}

$zip_path = str_replace('\\', '/', realpath($zip_path));
$filesize = filesize($zip_path);
$filename = substr(strrchr('/'.$zip_path, '/'), 1);
$extension = strtolower(substr(strrchr($zip_path, '.'), 1));

// use this unless you want to find the mime type based on extension
$mime = array('application/octet-stream');

header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE only headers
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))) {
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
} else {
  header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);


} // close $_POST check
娇纵 2024-08-23 23:02:04
  1. 您可以使用isset($_FILES['file1'])检查file1是否上传

  2. 循环自file1 到 file5

  3. 将文件从临时路径保存到永久路径

  4. 使用 http://php.net/manual/en/book.zip.php & http://php.net/manual/en/ref.zip.php

  5. 可选:删除上传的文件(使用 unlink)

  6. 强制将 zip 文件下载到浏览器。请参阅: http://php.net/manual/en/function.header.php

这很简单。享受!

  1. You can use isset($_FILES['file1']) to check if file1 is uploaded

  2. Loop from file1 to file5

  3. Save files from temporary paths to permanent ones

  4. Zip them using http://php.net/manual/en/book.zip.php & http://php.net/manual/en/ref.zip.php

  5. Optional: Delete the uploaded files (using unlink)

  6. Force download the zip file to the browser. See this: http://php.net/manual/en/function.header.php

It's quite simple. Enjoy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文