0

I am working on Php,And i want to know that is this possible to download "Two different files" on same click ? I tried with following code

$pth    =   file_get_contents(base_url()."path/to/the/file.pdf");
$nme    =   "sample_file.pdf";
$pth2    =   file_get_contents(base_url()."path/to/the/file2.pdf");
$nme2    =   "sample_file2.pdf";
force_download($nme, $pth);
force_download($nme2, $pth2);

 
amit
  • 1
  • 1
  • 18
  • 28

1 Answers1

2

No, on server side (does not matter the language) you can't trigger 2 different downloads in the same request. You can do it via javascript something like the following should work:

function downloadFile(file) {
    // Create a link and set the URL using `createObjectURL`
    const link = document.createElement("a");
    link.href = URL.createObjectURL(file);
    // the link is invisible so do not show on the page
    link.style.display = "none";
    // this ensures the file is downloaded even if is a html one
    // you can set to true, or specify with which name the file will be downloaded 
    link.download = file.name;

    // attach to the DOM so it can be clicked
    document.body.appendChild(link);
    // click the link
    link.click();
    // this should free memory, usefull if you download many
    // files without page reload
    URL.revokeObjectURL(link.href);
    // remove the link from the DOM
    document.body.removeChild(link);
}

downloadFile('http://mywebsite/dowload/file_1.pdf');
downloadFile('http://mywebsite/dowload/file_2.docx');
Roberto Braga
  • 569
  • 3
  • 8