Problems With Multiple Downloads Using Checkboxes

I’ve been trying to put together a page for our choir’s Members Only section to download multiple Training MP3s, using Checkboxes.

The files themselves are stored in a sub-directory of the main page, although for the sake of getting it working I could accept having them in the same directory instead, if necessary.

Please note: This is not my original coding, but something I found online. When placed in the Root Directory it works ok, but obviously that’s not where I need it to be.

The main site is based on a standard index.php format, located at the Site Root comprised of includes for Header, Body (of which this is one) & Footer.

I suspect the problem is something to do with the definition of the $file_folder.

I’ve added the echo “$file_folder”; line just to keep tabs on the value of $file_folder, which seems to look right.

The original defining value for $file_folder, based on running the page from the Site Root was $file_folder = “files/”; but because of the additional subdirectories, combined with having the page included in the root index.php, I’ve had to use an absolute address.

With my own extended address definition I’ve left the slash off the end, as if I leave it in, as demonstrated in the original script, the whole thing goes nuts, filling the browser with all sorts of garbage, slowing everything down & non responsive for ages before a refresh is possible. If left out, it displays, but simply doesn’t work.

The main script is placed at very top of page, before :
[php]

<?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"; 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]

And the rest of the script within the tags.

[php]


<?php if(!empty($error)) { ?>
<?php echo $error; ?>


<?php } ?>
[/php]

And one of the checkbox input lines:

[php]

Summer 2014 - Ladies
Dance A Cachucha All Parts Both Sops Both Altos
[/php]

…etc…

You will notice I’ve left the links in, as was the case in the original format of the page, and they still work fine.

The error message warning of no files selected works ok, so there’s nothing wrong with that side of things, but if a file is selected, then it just loops back to the beginning, clearing the selections.

Can anyone give any pointers as to what’s going wrong? Is it the definition of $file_folder (as I suspect), or is there something else I’m missing?

Thanks in advance for any help.

First I would remove this section

[php]$_SERVER[‘DOCUMENT_ROOT’][/php]

Being as you copied this and are trying to make it work, Document_Root IS NOT what you think it is. Set your location from the calling directory and see what happens. Otherwise you will get something like

var/user/root

I’ve tried that, whereby the echoed $file_folder reads “files/” but when running as an included file it still does nothing (just resets itself). However, if I run it directly, omitting the “index?page=” from the url, once again it goes nuts once a file is selected.

I just retried it again from the ROOT directory, and it went nuts again, so I totally reset it by pasting the original code back in:
[php]

<?php $error = ""; //error holder if(isset($_POST['createpdf'])){ $post = $_POST; $file_folder = "files/"; // folder to load files 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]
which got it working again FROM THE ROOT DIRECTORY. But when I tried saving the file, as is to the sub-directory ROOT/members/content/summer2014/downloads (directory where this page is)/(files - directory where the MP3s are stored), once again the page looks fine, but goes nuts when a file is selected, so it seems that it may have something to do with the including (although the same thing happens whether I access it using the “index.php?page=” part in the url or not.

It’s so infuriating not being able to get this working in the directory I want it to work in, as it’s just the script I need for the purpose.

(If you would like to see the page in action, I have it testing in the ROOT directory at http://www.newmusicmakers.com/index2.php & in the subfolder at http://www.newmusicmakers.com/index.php?page=members/content/summer2014/downloads/index2.php (of course that address uses the site root index includes, so the index.php?page="may be omitted).

Echo this
[php] $_SERVER[‘DOCUMENT_ROOT’][/php]
from the file you select the mp3’s from and from the include file and post them.

/home/users/web/b282/ipg.newmusicmakerscom

Same result in all cases:


http://www.newmusicmakers.com/members/content/summer2014/downloads/index2.php
http://www.newmusicmakers.com/index2.php

Which is why the version in my original post had .’/members/content/summer2014/downloads/files’; added to it.

Try this then,

[php] $file_folder = $_SERVER[‘DOCUMENT_ROOT’].’/members/content/summer2014/downloads/files/’;

[/php]

But, apart from the final slash, which caused total violent browser failure, isn’t that exactly the same as I had in my original post, which I was advised to remove?

I had also tried adding a slash to the beginning of the ‘value’ filenames in an attempt to replace the final slash, but that, not surprisingly, also resulted in the same crash.

It is. My last effort without having you send me files. Wrap EVERYTHING in a try catch statement and print out the error report.

[php]try {

//your code

} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), “\n”;
}[/php]

It doesn’t give an error message, as such.

If it doesn’t find a file (without the ending ‘/’) it just resets.
If it does find a file (presumably) everything goes crazy, with all sorts of garbage on the screen, so even if that try catchment was returning an error message among that lot, there wouldn’t be any way of finding it.

Sorry about this. For the past couple of months I had the code working fine, but for some reason it’s suddenly stopped, which doesn’t make any sense.

Pretty much as listed above, this is the code, currently placed at the very top of the page:
[php]<?php
$error = “”; //error holder
if(isset($_POST[‘download’])){

	$file_folder = "files/";	// folder to load files	summer2014/downloads/
	if(extension_loaded('zip')){	// Checking ZIP extension is available
		if(count($_POST['summer2014']) > 0){	// Checking files are selected
			$zip = new ZipArchive();			// Load zip library	
			$zip->name = "Downloads.zip";
						// Zip name
			if($zip->open($zip->name, ZIPARCHIVE::CREATE)){		// Opening zip file to load files
				
				foreach($_POST['summer2014'] as $file){				
					$zip->addFile($file_folder.$file);			// Adding files into zip
				}
			} else {
				$error .=  "* Sorry ZIP creation failed at this time<br/>";					
			}

			$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 <br/>"; //

	}else
		$error .= "* You dont have ZIP extension<br/>";
}

?>

etc.
[/php]

This is a typical tabled entry found on the page. As you can see, as well as the checkbox I use standard Hyperlinks as well, for individual downloads, which are all working fine:
[php]




All Parts


[/php]

The page I have this running (or not, as the case may be) is:
http://www.newmusicmakers.com/members/content/summer2014/downloads/index.php

I’m certain it’s staring me in the face, but can’t for the life of me see what it is.

Sponsor our Newsletter | Privacy Policy | Terms of Service