Download of .zip file runs a corrupted file php

后端 未结 7 420
误落风尘
误落风尘 2020-12-03 17:04

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

相关标签:
7条回答
  • 2020-12-03 17:44

    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()

    0 讨论(0)
  • 2020-12-03 17:55

    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

    0 讨论(0)
  • 2020-12-03 17:59

    I had similar problems for large zip files This works for me:

    In your php.ini, change to:

    • Upload_max_filesize - 1500 M
    • Max_input_time - 1000
    • Memory_limit - 640M
    • Max_execution_time - 1800
    • Post_max_size - 2000 M

    In your php file.

        $filename = "MyFile.zip";           
        $filepath='../downloads/'.$filename;    //your folder file path    
        header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");    
        header("Content-Length: ".filesize($filepath)); 
        header("Content-Disposition:attachment;filename=\"".basename($filepath)."\"");
    
    
        while (ob_get_level()) 
        {
         ob_end_clean();
         }
        readfile($filepath);   
        exit;
        ob_start ();
    
    0 讨论(0)
  • 2020-12-03 18:04

    @Pekka wins 2 beer! https://stackoverflow.com/a/2088288/9294174 In my case opening terminal and reading the file using nano shown me an xdebug message...

    0 讨论(0)
  • 2020-12-03 18:05

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 18:09

    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.

    0 讨论(0)
提交回复
热议问题