How to zip the selected files of a HTML form
<p>I have PHP page which lists some songs based on a criteria such as album year or something. I just placed a checkbox near to every song. When a visitor selects some check box and press submit button the form sends each value to a php file. In the PHP file the values are stored in an array.</p>

<pre><code>$files_to_zip=array();
for($i=0;$i&lt;count($_POST['song'];i++)
$files_to_zip[]=$_POST['song'][$i];

//'song' is the name of the checkbox in the HTML form
</code></pre>

<p>The problem is the PHP file does not create the zip file with the code i wrote.</p>

<pre><code>function create_zip($files = array(),$destination = '',$overwrite = true) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) &amp;&amp; !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip-&gt;open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip-&gt;addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip-&gt;numFiles,' files with a status of ',$zip-&gt;status;

//close the zip -- done!
$zip-&gt;close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}

$files_to_zip = array();
foreach($_POST['song'] as $fileabc)
{
$files_to_zip[]=$fileabc;
}
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
if($result==true)echo "Success"; else echo "failed";
</code></pre>

<p>This is the entire code I use in the PHP.</p>

<p>If I set the value for $files_to_zip dynamically such as,</p>

<pre><code>$files_to_zip=array('link1','link2','link3'..)
</code></pre>

<p>it works properly.</p>

<p>What is the bug in my code?</p>
