Jump to content

Downloading selected multiple file


Go to solution Solved by requinix,

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/311984-downloading-selected-multiple-file/
Share on other sites

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.

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.

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.