PHP Force Download Causing 0 Byte Files

后端 未结 8 1110
清歌不尽
清歌不尽 2020-12-10 16:11

I\'m trying to force download files from my web server using PHP. I\'m not a pro in PHP but I just can\'t seem to get around the problem of files downloading in 0 bytes in s

相关标签:
8条回答
  • 2020-12-10 17:07

    You need to specify the Content-Length header:

    header("Content-Length: " . filesize($filename));
    

    Also, you shouldn't send a Content-Transfer-Encoding header. Both of the HTTP/1.0 and HTTP/1.1 specs state that "HTTP does not use the Content-Transfer-Encoding (CTE) field of RFC 1521".

    0 讨论(0)
  • 2020-12-10 17:07

    The file opened ok for me when I changed the directory to the file location.

    $reportLocation = REPORTSLOCATION;
    
    $curDir = getcwd();
    
    chdir($reportLocation);
    
    if (file_exists($filename)) {
    
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Length: ' . filesize($filename));
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
    
        ob_clean();
        flush();
    
        readfile($filename);
    }
    chdir($curDir);
    
    0 讨论(0)
提交回复
热议问题