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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在PHP中实现下载功能。 用户可以下载单个或多个文件。 如果用户尝试下载单个文件,则文件会正常下载。 如果用户尝试下载多个文件,则所有文件都将转换为zip并下载。

但我的问题是如何传递文件名及其路径。 对于单个文件,我使用

  window.location = directories_path +“?action = download& files =”+ download; 
  传递给它 
 
 

下载可以是我可以传递文件的数组。 但我无法通过它们。 我的网址似乎相似

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

我甚至尝试用AJAX传递json格式的文件名。 但它不起作用。

下载过程的我的JS代码

  $(“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(“请选择要下载的任何文件”); 
} else {
 window.location = directories_path +“?action = download& files =”+ download; 
} 
   
 
 

我下载单个文件的php代码是

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

我的问题是有什么办法让我可以在url中传递一个对象数组(文件名和文件路径),这样我就可以相应地下载文件了 。 谢谢

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题