Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download of .zip file runs a corrupted file php

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?

like image 746
Troy Avatar asked Jan 18 '10 18:01

Troy


People also ask

Can ZIP file be corrupted after download?

Actually, ZIP files can get corrupted during the download process itself, without us having any contribution. It could also be that the ZIP file was already corrupted from other sources before you even downloaded it to your device.

How do I download a corrupted ZIP file?

Step 1 Launch File Repair Tool and select the "Repair ZIP File" option start to repairing the process. Step 2 Search corrupt ZIP file in local drive and select the specific corrupt ZIP files. Step 3 All corrupt ZIP files list in the result section and click the "Repair" button.

Why do my ZIP files keep getting corrupted?

ZIP files can get corrupted during the download process. If the download was interrupted, due to a power outage or an unexpected program closure even for a moment, unreadable data can end up becoming part of the downloaded ZIP file and make it difficult for the data to be extracted.

How can I force download a ZIP file in PHP?

For force download, you have to set the header correctly. When you are setting the header for download the Zip file that time <coce>Content-type and Content-Disposition correctly. In Content-Disposition , add the attachment, it will suggest the browser to download the file instead of displaying it directly.


2 Answers

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;
    }
}
like image 153
Gumbo Avatar answered Sep 20 '22 09:09

Gumbo


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.

like image 38
Pekka Avatar answered Sep 21 '22 09:09

Pekka