Jump to content

Foreach stops executing after 1 loop


pocobueno1388

Recommended Posts

I have a program where the user can select multiple categories, then multiple cities. It should loop through each category, then download a file for each chosen city for that category.

 

<?php

if ($_POST['export']){

$categories = $_POST['categories'];
$cities = $_POST['cities'];

foreach ($categories AS $category){

	foreach ($cities AS $city){

		$keyword = new keyword(FALSE, $category, $city);
		$keyword->export();

		$file = ("keywords.csv");
		header ("Content-Type: application/octet-stream");
		header ("Accept-Ranges: bytes");
		header ("Content-Length: ".filesize($file));
		header ("Content-Disposition: attachment; filename=$category - $city");
		readfile($file);

	}
}		
}

?>

 

I'm pretty sure the loop isn't the problem. I think it's all the headers that are for someone reason stopping the execution. It will download the FIRST file, but then completely stops. It should automatically start multiple downloads.

 

I'm thinking it's something to do with one of the headers...like only one download can happen at once.

 

Any help would be greatly appreciated, Thanks!

Link to comment
https://forums.phpfreaks.com/topic/206425-foreach-stops-executing-after-1-loop/
Share on other sites

You can only output one set of headers and the corresponding file contents per HTTP request.

 

You can either dynamically produce a set of 'download links' on a page so that the visitor can click on each of them or you would need to zip the contents of all the files into a single download.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.