2010-01-07 6 views
-4

Je la forme suivante:Formulaire PHP - télécharger zip

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

Je ne peux pas alors obtenir la valeur tic tac:

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

donne le résultat: on

veulent Cependant, je veux être capable de faire est de sélectionner ces options, et chaque option se rapporte à un fichier PHP, sur soumettre chaque fichier est combi dans un ZIP

Toute aide appréciée.

+2

Où êtes-vous coincé? Qu'avez-vous essayé jusqu'à présent? –

+3

Qu'est-ce qui est différent de cette question? http://stackoverflow.com/questions/2019864/download-form-select-files-combile-into-zip –

+0

Je ne sais pas comment gérer en PHP, en particulier comment dire à PHP quels fichiers j'ai sélectionnés dans le formulaire et pour obtenir les fichiers. – CLiown

Répondre

1

Tout d'abord, ajouter un champ de valeur à vos champs de formulaire et de les changer à un tableau:

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

Ensuite, 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 
1
  1. Vous pouvez utiliser isset ($ _ FILES [ 'file1']) pour vérifier si fichier1 est téléchargé

  2. boucle de fichier1 à fichier5

  3. Enregistrer les fichiers de chemins temporaires à permanents

  4. les Zip en utilisant http://php.net/manual/en/book.zip.php & http://php.net/manual/en/ref.zip.php

  5. Facultatif: Supprimez les fichiers téléchargés (en utilisant unlink)

  6. Forcez le fichier zip dans le navigateur. Voir ceci: http://php.net/manual/en/function.header.php

C'est assez simple. Prendre plaisir!

Questions connexes