Download multiple files

Asked

Viewed 706 times

2

There is the possibility to download multiple files in a single link. I have tried many ways and I have not achieved anything if someone can give a strength.

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;
}
}

has 03 files I am able to download only one. In query I’m putting the GROUP BY so that the return of the bank is not too extensive

inserir a descrição da imagem aqui

1 answer

1


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

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

Compressed file

Compact all files and a single compressed file (zip for example), at the end of the accounts, you will download all the files through just 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: one or one zip with all the files.

Source: https://stackoverflow.com/a/1754359/1628790

iframe

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

You’d have to use javascript, or the very PHP, to create a iframe invisible from every file you want to download. The iframe would contain only downloadable code. If you would like to download 5 files, 5 files would be created iframes.

Case managed via javascript, you can dynamically create and delete iframes. In addition, within the iframe, as it is a very specific case, you can manage via javascript error situations and calling parent page functions (the page that runs the iframe). Once the download is complete, delete the iframe of GIFT.

Some examples:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.