tivadarsubotica Posted January 8, 2021 Share Posted January 8, 2021 Hi, I want to download more than one file at a time. But now I can't download any of them that way. I want to solve it the way it is in gmail, i.e. I download either 1 file separately or if there are two or more, it is zipped. Thanks in advanced, T <!-- images --> <section class="card shadow mb-3"> <form action="index.php?lg=<?php echo $lng; ?>&c=text" method="post" enctype="multipart/form-data"> <header class="card-header navbar-custom"><h3 class="text-center"><?php echo $new_document_h3_images; ?></h3></header> <div class="row"> <?php $files = glob('./tekstovi/' . $user_txt_year . '/' . $user_txt_nr . '/' . $text_page . '/' . $user_txt_year . '_' . $user_txt_nr . '_' . $text_nr . '_' . $text_page . '_img_' . '*.{doc,docx,odt,pdf,jpg,JPG,jpeg,png,gif,psd,eps,ai,tiff,tif}', GLOB_BRACE); $dir = './tekstovi/' . $user_txt_year . '/' . $user_txt_nr . '/' . $text_page . '/'; //$index = '0'; for ($i = 0; $i < count($files); $i++) { $image = $files[$i]; ?> <div class="col-sm-4 py-2"> <div class="card h-100"> <input type='checkbox' style="position: absolute; left: 10px; top: 10px;" name='boxes[]' value='<?php echo basename($image); ?>' /> <?php echo '<img class="img-fluid" src="' . $image . '" alt="" />'; ?> <div class="card-body"> <p class="card-title"><?php echo basename($image); ?></p> </div> <div class="card-footer"><?php $stmt_captionreload = $pdo->prepare('SELECT * FROM `sn_images` WHERE sn_images_text_id = :sn_images_text_id AND CONCAT(`sn_images_filename`,`sn_images_basename`) = "' . basename($image) . '" '); $stmt_captionreload->bindValue(':sn_images_text_id', $text_id); $stmt_captionreload->execute(); $row_captionreload = $stmt_captionreload->fetch(PDO::FETCH_ASSOC); $one_caption = stripslashes($row_captionreload['sn_images_caption']); ?> <small class="text-muted"><?php echo $one_caption; ?></small></div> </div> </div> <?php }?> </div><!-- row--> <button type="submit" name="download" value="1" class="btn btn-success float-right">Download selected</button> <input type = 'hidden' name="sn_text_nr" value="<?php echo $text_nr; ?>"> </form> <?php if ($_POST['download'] == '1') { $index = '0'; echo 'Total count: ' . count($_POST['boxes']) . ''; while ($index < count($_POST['boxes'])) { if (isset($_POST['boxes'][$index])) { $file = $_POST['boxes'][$index]; $path = $dir . $file; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($path)); header('Content-Transfer-Encoding: binary'); header('Expires: 600'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($path)); flush(); readfile($path); } else { } $index++; } } ?> </section> <!-- /. images --> Quote Link to comment https://forums.phpfreaks.com/topic/311984-downloading-selected-multiple-file/ Share on other sites More sharing options...
requinix Posted January 8, 2021 Share Posted January 8, 2021 That normal technique you have in the code, with the headers, only works for a single file. It cannot be used for multiple files. You've kinda discovered that. Use ZipArchive with a temporary .zip file on the server. Add your files to it, send it, then delete it. One other thing: in your code before you start that work, turn off user aborts. Quote Link to comment https://forums.phpfreaks.com/topic/311984-downloading-selected-multiple-file/#findComment-1583705 Share on other sites More sharing options...
tivadarsubotica Posted January 8, 2021 Author Share Posted January 8, 2021 23 minutes ago, requinix said: That normal technique you have in the code, with the headers, only works for a single file. It cannot be used for multiple files. You've kinda discovered that. Use ZipArchive with a temporary .zip file on the server. Add your files to it, send it, then delete it. One other thing: in your code before you start that work, turn off user aborts. At the moment, for some reason, I can't download a single file with this script, but my problem is that I don't know how to solve it by downloading only one file for one file, zipping it for multiple files (with one submitt). I’m not very familiar with PHP programming language, and especially never downloading (plus zipped). I searched the internet for opportunities but couldn’t figure out how to solve it. Quote Link to comment https://forums.phpfreaks.com/topic/311984-downloading-selected-multiple-file/#findComment-1583706 Share on other sites More sharing options...
Solution requinix Posted January 8, 2021 Solution Share Posted January 8, 2021 Look at the examples and maybe try some Google searches for using PHP to download multiple files with a ZIP archive. There's a lot of information out there and it isn't too hard to find samples you can reference. Quote Link to comment https://forums.phpfreaks.com/topic/311984-downloading-selected-multiple-file/#findComment-1583707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.