1

I'm trying to download multiple files while parsing an array, this is a snippet of the Class I wrote. Please advise what I'm doing wrong.

It only downloads the first iteration.

  foreach($ad_row as $row){
                header("Content-type: application/msword");
                header("Content-Disposition: attachment;Filename={$title}.doc");

                $first = '<!DOCTYPE html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><body><table><tbody><tr><td>';
                $last = '</td></tr></tbody></table></body></html>';
                echo $first.$ad_doc->saveHTML($row).$last;
                ob_clean();
                flush();
                }//end foreach

Thanks

hayonj
  • 1,429
  • 3
  • 21
  • 28
  • 1
    You can't download multiple files in a single page. – Barmar Mar 27 '17 at 21:28
  • You need to do multiple ajax calls and one download per call. – Matt Mar 27 '17 at 21:33
  • @Barmar - I'm running it through with an array of ads. If I display all of them I get my Ads. Instead of one large html file I'm trying to download multiple files and save each as word document. But as soon as it download the first one it's finishes the page load instead of grabbing the next part. – hayonj Mar 27 '17 at 21:38
  • @mkaatman I'm not using any Javascript this is purely built with php. – hayonj Mar 27 '17 at 21:39
  • @hayonj I understand what you're trying to do, but it can't work. One HTTP request can only download one file. – Barmar Mar 27 '17 at 21:41
  • @hayonj See http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax for how to download a file using AJAX. You'll need to put the loop in the client, not the server. – Barmar Mar 27 '17 at 21:44
  • So what's the workaround if I'm not using JS ? How can I split up my request into 500 different request lol because that's what I need. In the end I'll have 500 word documents. – hayonj Mar 27 '17 at 21:45
  • 1
    How about putting them all into a ZIP archive, and downloading that? – Barmar Mar 27 '17 at 21:46
  • A zip file of all the documents will allow you to send multiple documents in one file. – Matt Mar 27 '17 at 21:48
  • Not using Javascript so Ajax is not good for me in this case. As for putting them in a zip I'd have to see if I can create word documents without downloading them add them to the the zip while still in the loop and download the zip at the end. Makes sense but I still need to figure this out. – hayonj Mar 27 '17 at 21:49
  • But also think about the poor client, why would you like to push 500 word documents at once? Also I think there is a number of allowed concurrent downloads per browser session and you would have to accept each of the files to allow them to be downloaded ... As mentioned by @Barmar a zip file might be the only reasonable solution – edi Mar 27 '17 at 21:52
  • kindly look at this [post](http://stackoverflow.com/questions/985431/max-parallel-http-connections-in-a-browser) – edi Mar 27 '17 at 21:55
  • This is not a live server. There is no client but me. I'm just trying to download all my Ads at once instead of manually creating them one by one. I don't care how long it takes to generate as long as the job is being done. I get its easier done with Ajax but I chose not to include any JS. – hayonj Mar 27 '17 at 21:55
  • then what about writing the documents to file system, manually zip them and then download it? – edi Mar 27 '17 at 21:57
  • That defies the while idea of writing this application. Thin of it like this : say you have a source code that contains 100

    éléments. You'll parse the page and each p element become a force downloaded word document. In a nutshell that's what I'm trying to accomplish

    – hayonj Mar 27 '17 at 22:02
  • There has to be a way. I tried did link [a]https://www.google.com/url?q=http://stackoverflow.com/questions/11491399/download-content-of-several-files-on-server-with-php&usd=2&usg=AFQjCNGVpGmJFA63tHlzv2DQEGg3mroCuw[/a] but it didn't work for me. – hayonj Mar 27 '17 at 22:14

0 Answers0