Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP可以';无法打开下载的zip,但服务器上的文件是可打开的_Php_Download - Fatal编程技术网

PHP可以';无法打开下载的zip,但服务器上的文件是可打开的

PHP可以';无法打开下载的zip,但服务器上的文件是可打开的,php,download,Php,Download,我使用ZipArchive创建zip文件。除了一件事——下载它作为附件外,它一切都很好。当我试图打开下载的文件时,7-zip说:“无法打开文件…作为存档”。保存在服务器上的文件一切正常。当我试图将下载的文件与存储在服务器上的文件进行比较时,文件的结尾有一点不同 简单地说:服务器上的存档打开了,但下载后却没有打开 我使用的代码是: $file='Playlist.zip'; if (headers_sent()) { echo 'HTTP header already se

我使用ZipArchive创建zip文件。除了一件事——下载它作为附件外,它一切都很好。当我试图打开下载的文件时,7-zip说:“无法打开文件…作为存档”。保存在服务器上的文件一切正常。当我试图将下载的文件与存储在服务器上的文件进行比较时,文件的结尾有一点不同

简单地说:服务器上的存档打开了,但下载后却没有打开

我使用的代码是:

$file='Playlist.zip';
    if (headers_sent()) {
        echo 'HTTP header already sent';
    } else {
        if (!is_file($file)) {
            header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
            echo 'File not found';
        } else if (!is_readable($file)) {
            header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
            echo 'File not readable';
        } else {
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
            header("Content-Type: application/zip");
            header("Content-Length: ".filesize($file));
            header("Content-Disposition: attachment; filename=".basename($file)."");
            header("Pragma: no-cache"); 
            header("Expires: 0");
            set_time_limit(0);
            $handle = fopen($file, "rb");
            while (!feof($handle)){
                echo fread($handle, 8192);
            }
            fclose($handle);
        }
    }

尝试使用
fpassthru()


对我有用。您的PHP和apache版本是什么?文件末尾的细微差别到底是什么?ob_clean()和flush()帮助了我。现在可以了:)解决了。
$file="Playlist.zip";
if (headers_sent()) {
    echo "HTTP header already sent";
} else {
    if (!is_file($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found");
        echo "File not found";
    } else if (!is_readable($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . " 403 Forbidden");
        echo "File not readable";
    } else {
        header($_SERVER['SERVER_PROTOCOL'] . " 200 OK");
        header("Content-Type: application/zip");
        header("Content-Length: " . filesize($file));
        header("Content-Disposition: attachment; filename=" . basename($file));
        header("Pragma: no-cache"); 
        $handle = fopen($file, "rb");
        fpassthru($handle);
        fclose($handle);
    }
}