2010-01-07 35 views
-4

我有以下形式:PHP表 - 下載到拉鍊

<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文件,提交每個文件被整合到一個ZIP中

任何幫助表示讚賞。

+2

你在哪裏卡住了?你試過什麼了? – 2010-01-07 11:44:43

+3

這個問題有什麼不同? http://stackoverflow.com/questions/2019864/download-form-select-files-combile-into-zip – 2010-01-07 11:46:42

+0

我不知道如何在PHP中管理,特別是如何告訴PHP我在表單中選擇了哪些文件和whiere來獲取文件。 – CLiown 2010-01-07 11:51:02

回答

1

首先,值字段添加到您的表單字段並將其更改爲一個數組:

<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