Download of .zip file runs a corrupted file php

2019-01-12 02:51发布

I'm trying to force a download of a protected zip file (I don't want people to access it without logging in first.

I have the function created for the login and such , but I'm running into a problem where the downloaded file is corrupting.

Here's the code I have:

$file='../downloads/'.$filename;
header("Content-type: application/zip;\n");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file).";\n");
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile("$file");
exit();

Here's the error: Cannot open file: It does not appear to be a valid archive.

The file downloads fine otherwise, so it must be something I'm doing wrong with the headers.

Any ideas?

5条回答
\"骚年 ilove
2楼-- · 2019-01-12 03:26

I bet two beers that a PHP error occurs, and the error message messes up the ZIP file. The requested file probably doesn't exist.

Open the ZIP file with notepad or a similar text editor, and find out what's wrong.

查看更多
聊天终结者
3楼-- · 2019-01-12 03:29

try this to find if there is any error

error_reporting(0);

Do not print anything before writing the headers; Run an ob_start() to of your script, after the code edit your headers and then ob_flush() and ob_clean()

查看更多
唯我独甜
4楼-- · 2019-01-12 03:31

This issue can have several causes. Maybe your file is not found or it can not be read and thus the file’s content is just the PHP error message. Or the HTTP header is already sent. Or you have some additional output that then corrupts your file’s content.

Try to add some error handling into your script like this:

$file='../downloads/'.$filename;
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-Transfer-Encoding: Binary");
        header("Content-Length: ".filesize($file));
        header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
        readfile($file);
        exit;
    }
}
查看更多
Explosion°爆炸
5楼-- · 2019-01-12 03:42

Late answer but maybe useful for users not being able to make force download work.
Add the following at the top of your php script

<?php
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
查看更多
三岁会撩人
6楼-- · 2019-01-12 03:49

Here is the solution create a file with name .htaccess write below line in that SetEnv no-gzip dont-vary

upload the file to your website. If you have the file already there then please make the above change in that

查看更多
登录 后发表回答