可以在PHP中创建zip存档,但无法正确下载2

可以在PHP中创建zip存档,但无法正确下载2,php,download,zip,Php,Download,Zip,我想对这篇文章发表评论,但我的声誉低于谭50,所以我不得不再次提出几乎相同的问题 我从解压中(从我的网站下载后)收到以下错误消息 找不到中央目录签名的结尾。此文件不是 zipfile,或它构成多部分存档的一个磁盘。在 后一种情况下,将在上找到中心目录和zipfile注释 此存档的最后一个磁盘。解压:找不到zipfile目录 在foo.zip或foo.zip.zip之一中,找不到foo.zip.zip,句号 在我的lampp服务器(testsetup)(at/opt/lampp/myproject

我想对这篇文章发表评论,但我的声誉低于谭50,所以我不得不再次提出几乎相同的问题

我从解压中(从我的网站下载后)收到以下错误消息

找不到中央目录签名的结尾。此文件不是 zipfile,或它构成多部分存档的一个磁盘。在 后一种情况下,将在上找到中心目录和zipfile注释 此存档的最后一个磁盘。解压:找不到zipfile目录 在foo.zip或foo.zip.zip之一中,找不到foo.zip.zip,句号

在我的lampp服务器(testsetup)(at/opt/lampp/myprojects/tmp/foo.zip)上,解压工作正常(没有错误消息)

我正在使用以下代码:

$filename = 'foo.zip';
$path = '/opt/lampp/myprojects/tmp/foo.zip';
if(!file_exists($path))
{
    die('Error: file not found');
}
else {
    $handle = fopen($path, "r");
    header('Content-Description: File Transfer');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path));

    flush();
    readfile($path);
    fclose($handle);



  

有谁能看到我的错误并帮助我解决这个问题吗?我试图修复它大约5小时,但无法解决问题。

使用以下命令将您的ZIP文件发送到用户/浏览器。没有理由先打开它

<?php
$filename = 'foo.zip';
$path = '/tmp/foo.zip';
if(!file_exists($path)) {
    die('Error: file not found');
}

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));

readfile($path);
ob_end_clean();
$FileBytes = filesize($temp_file);

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="WhateverYourFileNameIs.zip"');
header('Content-Length: ' . $FileBytes);

readfile($temp_file);

使用以下命令将ZIP文件发送到用户/浏览器。没有理由先打开它

ob_end_clean();
$FileBytes = filesize($temp_file);

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="WhateverYourFileNameIs.zip"');
header('Content-Length: ' . $FileBytes);

readfile($temp_file);

注释掉
$handle=fopen($path,“r”)
fclose($handle)只保留
读取文件($path)
.Comment out
$handle=fopen($path,“r”)
fclose($handle)只保留
读取文件($path)