0

Thanks to this topic I'm able to generate one single text file and download it on the fly. But I would like to generate two separated text files. My code seems like this:

        $status = //math

        if(status)

           header("Content-type: text/plain");
           header("Content-Disposition: attachment; filename=config.txt");

 //...config text

 include 'test.php'; 

Test.php file:

<?php 

    $timeout = "time";

    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=timout.xml");

    echo $timeout;
?>

the timeout.xml text is being included inside config.txt, generating only the timout.xml file to download. Is there a way to generate two separated text files to download on the fly?

Community
  • 1
  • 1
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • Not within the same request, no. You'll have to make two separate download requests. – deceze Sep 30 '14 at 13:45
  • @deceze I thought I could do including another php file, that was my idea, so how may I call two different requests? considering I'm using only one button is it possible? – Victor Oliveira Sep 30 '14 at 13:46
  • You could do this if you generated the two files and put them in a tarball/zip file and output that instead – Brian Sep 30 '14 at 13:46
  • @VictorOliveira a request against the server is meant to return a single file (normally HTML or JSON or something like that), but you can alternatively return a different file, but the rule is you can only return one file for one request. That's how REST works. – Brian Sep 30 '14 at 13:47
  • @Brian zips not really an option, but I read about this few minutes ago; thanks – Victor Oliveira Sep 30 '14 at 13:47
  • Think about what that would look like in the browser, it doesn't care that you `include` some file server-side. You can't receive two files with a click on one link. You could trigger two downloads via Javascript if you wanted. – deceze Sep 30 '14 at 13:48
  • @deceze could u post some code to guide me? thus I could accept as an answer – Victor Oliveira Sep 30 '14 at 13:49
  • 3
    What @deceze said is a viable alternative to you for sure. You could use JS on a link to trigger two different requests to the server, which would let you technically trigger two downloads off a single click, but it would still execute **two PHP scripts** on the server side, or the single one with two different sets of request variables. – Brian Sep 30 '14 at 13:50
  • might be you can get some help from this http://stackoverflow.com/questions/4993468/php-header-to-download-multiple-files-in-loop/19972789#19972789 – Ram Sharma Sep 30 '14 at 14:00

1 Answers1

0

You cant do it because HTTP doesnt support the downloading of multiple files in a single click..The best option would be zipping the files on the server and transfering it as said here.

Or you can use javascript to on the link to request two request to the server and which lets you fetch the data from the single click as @deceze and @brian said in the comment

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26