简体   繁体   English

使用PHP下载多个文件

[英]Multiple files download using PHP

I am trying to download multiple files using PHP. 我正在尝试使用PHP下载多个文件。 I know it can only be done by using a zip archive, but I dont want all the directories and subdirectories to be included in the downloaded folder, I just need the files. 我知道只能使用zip存档来完成此操作,但是我不希望所有目录和子目录都包含在下载的文件夹中,我只需要这些文件。

I tried using "basename()" but it wont download anything when I do that, following is my code. 我尝试使用“ basename()”,但这样做时不会下载任何内容,以下是我的代码。

I also tried making a new array of just the filenames extracted by using "basename()", but it wont download still. 我还尝试制作仅使用“ basename()”提取的文件名组成的新数组,但仍无法下载。

        if ( extension_loaded( 'zip' ) ) {
        $zipFolder = new ZipArchive();
        $zipName = time().".zip";

        if ( $zipFolder->open( $zipName, ZIPARCHIVE::CREATE ) !== TRUE ) {
            echo "Sorry ZIP creation failed at this time";
        }

        foreach ( $filepathArray as $f ) {
            $zipFolder->addFile($f);
        }

        $zipFolder->close();

        if ( file_exists( $zipName ) ) {
            header( 'Content-type: application/zip' );
            header( 'Content-Disposition: attachment; filename="' . $zipName . '"' );
            readfile( $zipName );
            unlink( $zipName );
        }
    }

This is my dashboard screenshot: 是我的仪表板截图:

As you can see the path it quite long, hence I don't need all those folders, I just need the files. 如您所见,路径很长,因此我不需要所有这些文件夹,我只需要文件。

HTTP doesn't support multiple files in a single download. HTTP一次下载不支持多个文件。 So make base page with a javascript timeout that redirects the user to each of the files, one at a time. 因此,使用javascript超时制作基本页面,该超时将用户一次重定向到每个文件。

Example: 例:

   <script type="text/javascript">
    var file_list = new Array(
      'http://www.example.com/filename1.file',
      'http://www.example.com/filename2.file',
      'http://www.example.com/filename3.file'
    );
    for (var i=0; i < file_list.length - 1; i++) {
      setTimeout(function () {
         document.location = file_list[i];
      }, i * 1000);
    }
    </script>

    <h1>Please wait</h1>
    <p>Your download will start in a moment.</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM