【问题标题】:Downloading multiple files using PHP ZipArchive使用 PHP ZipArchive 下载多个文件
【发布时间】:2015-11-30 16:33:31
【问题描述】:

我正在尝试随机下载一些文件。

我很抱歉,因为我之前发布了它,但有人可以详细解释我做错了什么吗?

我似乎无法调试它,因为我知道最小的 php。

<?php 
$rootPath = realpath('./uploads');
//make zip file
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$nfiles = glob($rootPath.'*.{aiff}', GLOB_BRACE);     
$files = array_rand($nfiles, 20);
foreach($files as $file)  {
    $pathtofile = $nfiles[$file];
    if (!$file->isDir())
    {
        // Get path for current file
        $filePath = $pathtofile->getRealPath();
        $relativePath = str_replace($rootPath, "", $file);
        // Add file to zip
        $zip->addFile($filePath, $relativePath);
    }
}
//-------------------------------------------//
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment;
filename='.'generatedsounds.zip');
header('Content-Length: ' . filesize('file.zip'));
readfile('file.zip');
if(file_exists('file.zip'))
{
    unlink('file.zip');
}
?>

【问题讨论】:

  • 有什么问题?什么不工作?你得到什么错误?
  • 你的问题是什么?运行代码时会发生什么? 是否运行?
  • 我所做的是这样的:&lt;a href="/my/path/file.php" download=""&gt;Download file&lt;/a&gt;。为我工作:)
  • 改进的代码格式、改进的标签和改进的标题。

标签: php php-zip-archive


【解决方案1】:

您正在调用未定义的 -&gt;isDir() 方法,并且您正在调用同样未定义的 -&gt;getRealPath()

你得到所有文件,然后从数组中选择 20 个随机文件(为什么??)

当您重写标头时,错误不会显示在浏览器中,请尝试不重写标头,这意味着它会保留为 html 标头并进行测试,直到您获得二进制文件输出,然后将标头添加回来,这样您就有了工作代码.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2023-04-07
    • 2012-03-04
    • 1970-01-01
    • 2010-12-17
    • 2015-07-05
    相关资源
    最近更新 更多