I would really appreciate help here. I think I'm just missing something small but I just can't figure it out.
I have 2 questions about the following code:
<?
if(isset($_POST['download'])) {
$x = 0;
$download_path = dirname(__FILE__).'/';
echo "<b>Downloading from $download_path:</b><br />";
$file = array();
while($x <= $_POST['download']) {
$files1 = "file".$x;
if (isset($_POST[$files1])){
echo $_POST[$files1]."<br />";
$file[$x] = str_replace(" ","_",$_POST[$files1]);
}
$x++;
}
if($_POST['playlist']) {
$playlist = $_POST['playlist'].".zip";
}
else {
$playlist = "Playlist.zip";
}
zipFilesAndDownload($file,$playlist,$download_path);
}
//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
//create the object
$zip = new ZipArchive;
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::OVERWRITE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
echo $file_names[0];
$zip->addFile($file_path.$file_names,$file_names[1]);
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
}
$zip->close();
//then send the headers to foce download the zip file
/*header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");*/
readfile($archive_file_name);
exit;
}
?>
1.) It's not creating the archive. Can anyone tell why?
2.) I tried some troubleshooting around line 34/35 to see if all my variables were being passed correctly into the function. It seems that $file[] is being passed correctly because when I echo $file_names it just says Array, but when I try to echo $file_names[0] I get undefined offset which doesn't make any sense. How does it know it's an array then? The $file array outside the function contains values cause when I echo them they show up with the right result.
I would appreciate any help. Thanks!