PHP Force Download Causing 0 Byte Files

后端 未结 8 1108
清歌不尽
清歌不尽 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

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

提交回复
热议问题