Jump to content

Multiple Downloads Using Checkboxes


FourPart

Recommended Posts

I'm trying to put together a page in the 'Members Only' section for my choir. The page involves using checkboxes in order to download multiple MP3s for training purposes.The main part of the script is at the very top of the page, before the :<?php$error = ""; //error holder if(isset($_POST['createpdf'])){ $post = $_POST; $file_folder = $_SERVER['DOCUMENT_ROOT'].'/members/content/summer2014/downloads/files'; // folder to load filesecho "$file_folder"; // I just put this in to keep tabs on the value of $file_folder if(extension_loaded('zip')){ // Checking ZIP extension is available if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected $zip = new ZipArchive(); // Load zip library $zip_name = time().".zip"; // Zip name if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files $error .= "* Sorry ZIP creation failed at this time

"; } foreach($post['files'] as $file){ $zip->addFile($file_folder.$file); // Adding files into zip } $zip->close(); if(file_exists($zip_name)){ // push to download the zip header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="'.$zip_name.'"'); readfile($zip_name); // remove zip file is exists in temp path unlink($zip_name); } }else $error .= "* Please select file to zip

"; }else $error .= "* You dont have ZIP extension

"; }?>...<?php if(!empty($error)) { ?>

<?php echo $error; ?>

<?php } ?>Summer 2014 - Ladies Dance A Cachucha All Parts Both Sops Both Altos (...etc)The site is laid out within a standard index.php at the Site Root, containing includes for Header, Body (of which this page is one) & Footer.I feel certain that the problem lies somewhere in the $file_folder = $_SERVER['DOCUMENT_ROOT'].'/members/content/summer2014/downloads/files'; // folder to load filesline. The original script that I found online was clearly meant for usage at the Site Root, with $file_folder simply being set as:$file_folder = "files/";Which works fine when tested in the root folder - but, of course, that's not where I want it.With my modified definition of $file_folder I can see from the echo "$file_folder"; that the address is set correctly. However, if I add the slash to the end of the definition (making ...."/files" into .... "/files/", then the whole thing goes nuts. Without it screen comes up as it should do, and even the error message when no file is selected comes up in the right place (so that bit's working ok).You will also note that I have left the original hyperlinks in (as the page was in the first place before I tried using checkboxes), and they are also working fine.Is the problem, as I suspect, within the definition of $file_folder, or am I overlooking something else?I emphasise that I am a relative beginner to PHP, and that this checkbox script is not my own. It seems a fairly straightforward script & I can understand what most of it does, but I am totally flummoxed on this one.Can anyone help?
Link to comment
Share on other sites

wherever you copy/pasted your post from dropped the newlines. i recommend that you post your original content, question and code, putting your code in the forum's


bbcode tags (the edit form's <> button), so that someone will even attempt to look at your problem.

 

Link to comment
Share on other sites

In your foreach loop below, try adding another echo statement:

foreach($post['files'] as $file){
     echo "<div>$file_folder$file</div>";  //<-- ADD THIS
     $zip->addFile($file_folder.$file);  // Adding files into zip 
}
 
Does it display the correct path for the files?
 
 
Also note the following quote from the manual (http://www.php.net/manual/en/function.header.php):

 


Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

 

Having the echo statements to see what the path contains will mess up your header() calls later on.

 

 

For those interested, here is FourPart's code broken up. Of course, it may not be exact.  :happy-04:

<?php
$error = ""; //error holder
if(isset($_POST['createpdf'])){
     $post        = $_POST;
     $file_folder = $_SERVER['DOCUMENT_ROOT'].'/members/content/summer2014/downloads/files'; // folder to load files
     echo "$file_folder"; // I just put this in to keep tabs on the value of $file_folder
     if(extension_loaded('zip')){ // Checking ZIP extension is available 
          if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected 
               $zip      = new ZipArchive();  // Load zip library 
               $zip_name = time().".zip";  // Zip name 
               if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){  // Opening zip file to load files 
                    $error .= "* Sorry ZIP creation failed at this time"; 
               }
               foreach($post['files'] as $file){
                    print "<div>$file_folder$file</div>";
                    $zip->addFile($file_folder.$file);  // Adding files into zip 
               } 
               $zip->close(); 
               if(file_exists($zip_name)){ // push to download the zip 
                    header('Content-type: application/zip'); 
                    header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
                    readfile($zip_name); // remove zip file is exists in temp path 
                    unlink($zip_name); 
               } 
          } else 
               $error .= "* Please select file to zip"; 
     }else 
          $error .= "* You dont have ZIP extension"; 
} 
?> . . . <?php 
if(!empty($error)) {
     echo $error;
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.