0

我正在尝试制作收集所有文件的文件下载脚本,并下载一个包含该文件集合而不是所有目录结构的 zip 文件。

我在 download/folder1/folder1/filename.extension 中有文件

这是我的 PHP 代码:

if( !extension_loaded('zip') ){
    echo "<script>alert('Error: Please contact to the Server Administrator!');</script>";
    exit;
}

$zip = new ZipArchive;
if( $zip->open($zipname, ZipArchive::OVERWRITE) === TRUE ){
    foreach( $files as $file ){
        $zip->addFile( BASE_PATH.$file_path.'/'.$file, $file );
    }
    $zip->close();
} else {
    echo "<script>alert('Error: problem to create zip file!');</script>";
    exit;
}

这段代码给了我这样的结构:

在此处输入图像描述

它给出了 wamp 的完整路径(包括路径目录和文件)和文件,我只想添加文件而不是目录..

有人可以告诉我我错过了什么吗?

4

1 回答 1

0

每次下载链接都带有唯一的下载密钥时,我只需添加unique_keydownload.zip 文件的名称及其工作...

// $db_secret_key Random Unique Number
$zipname = $db_secret_key."_download.zip";

if( !extension_loaded('zip') ){
     echo "<script>alert('Error: Please contact to the Server Administrator!');</script>";
     exit;
}


$zip = new ZipArchive;
if( $zip->open($zipname, ZipArchive::CREATE) === TRUE ){
     foreach( $files as $file ){
           $zip->addFile( BASE_PATH.$file_path.'/'.$file, $file );
     }
     $zip->close();

     // Force Download
     header("Content-Type: application/zip");
     header("Content-disposition: attachment; filename=$zipname");
     header("Content-Length: ".filesize($zipname)."");
     header("Pragma: no-cache");
     header("Expires: 0");
     readfile($zipname);
     exit;
} else {
     echo "<script>alert('Error: problem to create zip file!');</script>";
     exit;
} 
于 2013-05-09T07:15:06.100 回答