douzheng9221 2018-08-10 12:20
浏览 74
已采纳

如何为php下载传递多个文件名

I am trying to implement download functionality in PHP. A user can download single or multiple files. If user try to download single file, the file gets downloaded normally. If user tries to download multiple files, all the files will be converted to zip and get downloaded.

But my problem is how to pass the gruop of filenames along with their paths. For a single file I passed it using

window.location = directories_path + "?action=download&files="+download;

download can be an array where I can pass files. But I am unable to pass them. My url appeared alike

localhost/proj/Base?action=download&files=[object Object]

I even tried with AJAX passing file names in json format. But it didn't work.

My JS code for the download process

$("input:checked").each(function(){
  if($(this).siblings('a').length > 0)
  {
    download.push({
      name: $(this).siblings('a').text(),
      path: $(this).siblings('a').attr('href'),
    });
  }
});
if( checked == 0 ) {
  alert("Please select any file to download");
} else {
  window.location = directories_path + "?action=download&files="+download;
}

My php code for downloading single file is

header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
readfile($file);

My question is Is there any way so that I can pass an array of objects(filename and filepath) in url so that I can download the files correspondingly. Thanks

  • 写回答

1条回答 默认 最新

  • douxuan3095 2018-08-10 15:14
    关注

    Thanks @ADyson and remaining all others for help. Finally this is how I implemented it. I am posting it so that it might be useful to someone.

    I made an AJAX post request and returned the link. And finally changed the window.location to that specific link for multiple files. But for single file downloads, It may not be possible sometimes. For Eg: If you change the location to PNG image. The browser opens the PNG image instead of downloading it. So, I preferred HTML anchor download tag for single file downloads.

    This is my JQuery code:

    $(".uploaded_files").on('click', '#download', function(event){
    download = [];
    $("input:checked").each(function(){
      if($(this).siblings('a').length > 0)
      {
        download.push({
          name: $(this).siblings('a').text(),
          path: $(this).siblings('a').attr('href'),
        });
      }
    });
    if( checked == 0 ) {
      alert("Please select any file to download");
    } else if( checked == 1) {
      $("body").append($('<a href="'+ download[0]['path']+'" download id="download_single"></a>'));
      console.log(download);
      document.getElementById('download_single').click();
      $("a#download_single").remove();
    } else {
      alert("The download begins in few seconds");
      $.post('Base.php', {
        action: 'download',
        files: download //array of objects of files
      }, function(link) {
        window.location = link;
      });
    }
    

    });

    This is my PHP code for downloading multiple files

    $zip = new ZipArchive();
    $zip_file = 'tmp/'.md5("download".rand(1,1000000)."zipfile").".zip";
    if( $zip->open($zip_file,  ZipArchive::CREATE))
    {
      foreach($files as $file)
      {
        $file['path'] = explode('uploads/', $file['path'])[1];
        $file['path'] = 'uploads/'.$file['path'];
        $zip->addFile($file['path'], $file['name'] );
      }
      $zip->close();
      header('Content-disposition: attachment; filename='.$zip_file.'');
      header('Content-type: application/zip');
      echo "http://localhost/".$zip_file;
      die();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?