pocobueno1388 Posted July 1, 2010 Share Posted July 1, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/206425-foreach-stops-executing-after-1-loop/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2010 Share Posted July 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206425-foreach-stops-executing-after-1-loop/#findComment-1079835 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 Try output buffering, maybe? Or add each file to a compressed archive and download just the archive? Just some thoughts, not sure how good they are . . . Quote Link to comment https://forums.phpfreaks.com/topic/206425-foreach-stops-executing-after-1-loop/#findComment-1079837 Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2010 Author Share Posted July 1, 2010 Okay, the zip file solved my problem. Thanks for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/206425-foreach-stops-executing-after-1-loop/#findComment-1079849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.