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!

Try on line 34 the following code and tell us the results:

echo '<xmp>'; print_r($file_names); echo '</xmp>';

That should dump the keys and values of the array. They don't always have numbers between the brackets.

hmm that's weird. the keys go 2,4,5,6,7,8. any reason why this might be? i think i'm incrementing x correctly.

Try replacing line 11 with the following:

$file[] = str_replace(" ","_",$_POST[$files1]);

That worked! I've always done arrays that way, and that's never happened to me before. That's so strange. Thank you very much!

Unfortunately it's still not making the zip archive. Can anyone spot any reason why that might be? I thought it maybe had something to do with permissions, but that's not the case.

The original code I posted has the headers commented out, but that was just to try and see what was going on with the array.

Nevermind I figured it out. My filenames were wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.