Download multiple files

2

There is the possibility of downloading multiple files in a single link. I have tried in many ways and I have not obtained results if someone can give a force.

In the image the termosPedidoId (78)

<?php

use App\Controllers\DB\Conn;
use App\controllers\Controller\Read;
use App\Controllers\Controller\Util;

$PDO = new Conn;
$Read = new Read;
$Util = new Util;

$result = $PDO->getConn()->prepare("SELECT * FROM tb_pedido_termos GROUP BY termosPedidoId");
$result->execute();
$dados = $result->fetchAll(PDO::FETCH_ASSOC);
foreach ($dados as $retorno){

    ?>
<table>
    <tr>
        <td><?php echo $retorno['termosPedidoId'] ?></td>
        <td>
            <a href="models/downloadUpload.php?id=<?php echo $retorno['termosPedidoId'] ?>">Download</a>
        </td>
    </tr>
</table>
<?php

}


// Arquivo que faz o download 

<?php
require '../../vendor/autoload.php';
use App\Controllers\DB\Conn;
$PDO = new Conn;

$pasta = '../uploadTermos/';
$result = $PDO->getConn()->prepare("SELECT * FROM tb_pedido_termos WHERE termosPedidoId=".$_GET['id']."");
$result->execute();
foreach ($result as $res){

if(isset($_GET['id']) && file_exists("{$pasta}/".$res['termosPedidoName'])){
    $file = $res['termosPedidoName'];
    $type = filetype("{$pasta}/{$file}");
    header("Content-Description: File Transfer");
    header("Content-Type:{$type}");
    header("Content-Disposition: attachment; filename=$file");
    readfile("{$pasta}/{$file}");

    $handler = fopen("{$pasta}/{$file}");
    if ($handler) {
        while (!feof($handler)) {
            $buffer = fgets($handler, 4096);
            echo $buffer;
        }
        fclose($handler);
    }
    exit;
}
}

There are 03 files I'm only able to download one. In the query I am putting the GROUP BY so that the return of the bank is not very extensive

    
asked by anonymous 10.07.2018 / 14:35

1 answer

1

This is not possible, at least not in the "orthodox" way. The HTTP protocol is designed to send only one file per request.

There are two ways (I'm in doubt if the second is still possible) to download more than one file per link.

Archive

Compress all files and a single compressed file ( zip for example), in the end, it will download all the files through only one file.

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

If you analyze e-mail services (Outlook, Gmail, etc ...), you will notice that you have two download options: either unitarily or a zip with all files.

Source: link

iFrame

In the past this worked, however, I'm not sure if browsers still allow such a solution.

You would have to use javascript , or PHP itself, to create an invisible iframe of each file you want to download. The iframe would contain only the download code. If you would like to download 5 files, 5 iframes would be created.

Managed case via javascript , you can dynamically create and delete the iframes . Also, within the iframe itself, as is a very specific case, you can manage via javascript error situations and call functions from the parent page (the page that runs iframe ). Once the download is complete, delete the iframe from the DOM.

Some examples:

10.07.2018 / 14:56