Download Selected Files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mpresley
Forum Newbie
Posts: 1
Joined: Thu Jul 21, 2011 3:03 pm

Download Selected Files

Post by mpresley »

I have downloaded a file from pclzip so that I can zip selected files and then get them to download. However, I am having trouble getting them to zip nor download. I also am having problems enabling and disabling the check boxes. I was trying to set it up to where if there was that kind of file was in there then that checkbox would be enabled. However, If I enable the video checkbox the two Misc checkboxes below that become enabled as well. I dont understand why that is happening.

Code: Select all

<?php

 

	//if the form has been submitted
	if(isset($_POST['submit']))
	{
		//automatically assign variables
		foreach($_POST as $key=>$value)
		{
			//if an info boxes was checked,
			//this will give as array called $info,
			//containing the product numbers for each selected item
			//same goes for hires and video
			if(!empty($_POST[$key]))
			{
				//list safe keys
				if($key=="hires1"||$key=="hires2"||$key=="lores1"||$key=="lores2"||$key=="video"||$key=="misc1"||$key=="misc2"||$key=="productinstructions")
				{
					//assign var name based on key name
					${$key}=$_POST[$key];
				}
			}
		}
		
		  //we can now see what was checked, and do anything we want with each item
        
        if(isset($hires1))
        {
            echo "<h3>Hi ResPhotos1 requested:</h3>";
            foreach($hires1 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
        
        if(isset($hires2))
        {
            echo "<h3>Hi Res Photos2 requested:</h3>";
            foreach($hires2 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
        
        if(isset($lores1))
        {
            echo "<h3>Lo Res Photos1 requested:</h3>";
            foreach($lores1 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
         if(isset($lores2))
        {
            echo "<h3>Lo Res Photos2 requested:</h3>";
            foreach($lores2 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
         if(isset($productinstructions))
        {
            echo "<h3>Product Instructions requested:</h3>";
            foreach($productinstructions as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
         if(isset($video))
        {
            echo "<h3>Videos requested:</h3>";
            foreach($video as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
         if(isset($misc1))
        {
            echo "<h3>Misc1 requested:</h3>";
            foreach($misc1 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
         if(isset($misc2))
        {
            echo "<h3>Misc2 requested:</h3>";
            foreach($misc2 as $file)
            {
                if(in_array($file,$products))
                    echo $file . "<br />";
            }
        }
        
    }
	//here we echo out all the products

	//echo out form and title
	echo "<br /><h2>Products:</h2>";
	echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" />";
	//define products directory
	$dir="products";
	//define directory handler
	$dh=opendir($dir);
	//read through the directory
		while(($file=readdir($dh))!==false)
		{
		//if the file is not . or .., do stuff with it
		

		if($file!=="."&&$file!=="..")
		{
			echo '<div id="directory">';
			echo "<br /><span style=\"font-weight:bold;\">$file</span><br /><br />";
			//echo image with hi_res link
			echo '<div id="thumb">';
			echo "<img src=\"$dir/$file/thumb$file.jpg\" alt=\"\" />";
			echo "<a href=\"$dir/$file/hires1-$file.jpg\" rel='shadowbox' alt=\"\" />Enlarge</a><br /><br />";
			echo "</div>";
			echo '<div id="productdescription">';
			$text = file_get_contents("$dir/$file/$file.txt");
			echo $text;
			echo "</div>";
			echo '<div id="downloadcontent">';
			echo "<input type=\"checkbox\" name=\"hires1[]\" value=\"hires1-$file\" /> High Res Photo 1 <br />";
			echo "<input type=\"checkbox\" name=\"lores1[]\" value=\"lores1-$file\" $disabled /> Low Res Photo 1 <br />";
			echo " <input type=\"checkbox\" name=\"hires2[]\" value=\"hires2-$file\" $disabled /> High Res Photo 2 <br />";
			echo "<input type=\"checkbox\" name=\"lores2[]\" value=\"lores2-$file\" $disabled /> Low Res Photo 2<br />";
			echo "<input type=\"checkbox\" name=\"productinstructions[]\" value=\"$file\" $disabled /> Product Instructions<br />";
			//check if video file exists, assign $disabled to either nothing or the disabled property
			$disabled=(file_exists("$dir/$file/video$file.mov")) ? "" : "disabled=\"disabled\"";
			echo "<input type=\"checkbox\" name=\"video[]\" value=\"video-$file\" $disabled /> Video<br />";
			echo "<input type=\"checkbox\" name=\"misc1[]\" value=\"$file\" $disabled /> Misc<br />";
			echo "<input type=\"checkbox\" name=\"misc2[]\" value=\"$file\" $disabled /> Misc";
			echo "</div>";
			echo "<hr />";
			echo "</div>";
			}
	}
	//submit button
	echo "<input type=\"submit\" name=\"submit\" style=\"position:relative;left:100px;\" />";
	echo "</form>";
	
require_once("pclzip.lib.php");
    $zip=new PclZip("archive.zip");
    $files=array("file.txt","file2.txt");
    $handler=$zip->create($files);
    if ($handler==0)
{
echo ("Error : ".$zip->errorInfo(true));  
}

   
?>

               


I have attached a recent screenshot so you can see what I have and what I am trying to achieve. The look is there I just need the download to function and the checkboxes to function. The misc categories are catch all for files that dont belong in any of the other categories. So, how would I write code for that as well
Post Reply