mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

How to download file in post method ? #256

Closed yogeshsaroya closed 1 year ago

yogeshsaroya commented 4 years ago

@mk-j How to download file in post method without saving file at server?

mk-j commented 4 years ago

PHP_XLSX_Writer uses ZipArchive, which writes a temporary file to the server. So there is always a temporary file saved on the server at some point.

NitemareReal commented 4 years ago

I wrote this method to download file:

public function download(string $filename){
        $buffer = $this->writeToString();
        header('Content-Description: File Transfer');
        if (headers_sent())
            $this->Error('Some data has already been output to browser, can\'t send XLSX file');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: public, must-revalidate, max-age=0');
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream', false);
        header('Content-Type: application/download', false);
        header('Content-Type: application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', false);
        if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
            // don't use length if server using compression
            header('Content-Length: '.strlen($buffer));
        }
        header('Content-disposition: attachment; filename="'.$filename.'"');
        print $buffer;      
    }
LrntL commented 2 years ago

@NitemareReal Thank you!