Why is my downloaded file is always damaged or corrupted?

后端 未结 5 938
故里飘歌
故里飘歌 2020-12-06 14:42

I have a very weird problem with my download script

it basically

1.gets a file id with "GET" method

2.gets the name and location

相关标签:
5条回答
  • 2020-12-06 15:01

    Your script may contain NOTICE or WARNING, try disabling error reporting on top of your script:

    error_reporting(0);
    
    0 讨论(0)
  • 2020-12-06 15:02

    Make sure you do not have any code executed after the last line readfile(FILE_NAME)

    I had to add die(); or exit(); as the last line, because MVC framework continues to render the view after readfile

    0 讨论(0)
  • 2020-12-06 15:16

    That seems allot of code just to force-download, here is a nice function I use all the time. It will handle files over 2GB too.

    <?php 
    $file_id = (isset($_GET['id']) && (int)$_GET['id'] != 0) ? (int)$_GET['id'] : exit;
    
    /*finding file info*/
    $file = comon::get_all_by_condition('files', 'id', $file_id);
    $path = $file['location'] . '/' . $file['file_name'];
    
    if (!is_file($path)) {
        echo 'File not found.('.$path.')';
    } elseif (is_dir($path)) {
        echo 'Cannot download folder.';
    } else {
        send_download($path);
    }
    
    return;
    
    //The function with example headers
    function send_download($file) {
        $basename = basename($file);
        $length   = sprintf("%u", filesize($file));
    
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $basename . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . $length);
    
        set_time_limit(0);
        readfile($file);
    }
    ?>
    
    0 讨论(0)
  • 2020-12-06 15:19

    Very nice, helpful also...

    but there is a problem in your code -

    header('Content-Disposition: attachment;
    filename="'.basename($file).'"';
    

    please change it with this following -

    header('Content-Disposition: attachment;
    filename="'.basename($file).'"');
    

    you are forgot to close it.

    0 讨论(0)
  • 2020-12-06 15:23
        if (file_exists($file)) {
    set_time_limit(0);
            header('Connection: Keep-Alive');
        header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="'.basename($file).'"');
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
    
    
    }
    
    0 讨论(0)
提交回复
热议问题