PHP通过echo发送0字节的excel文件

我正在使用MySQL数据库中的数据在单个string$ output中创build一个非常大的表。 在PHP脚本的最后,我发送了两个头文件:

header("Content-type: application/msexcel"); header("Content-Disposition: attachment; filename=action.xls"); 

问题是当大桌子有大约55000行时。 发生这种情况时,发送的文件是0字节,用户打开一个空文件。 我通过这个标题后发送文件:

 echo $output; 

当表没有太多的行时,文件发送工作。 如何以这种方式发送文件,string$输出的大小并不重要?

 header("Content-type: application/msexcel"); header("Content-Disposition: attachment; filename=action.xls"); 

将这些行移到html表之前的页面或脚本,它将开始工作

在另一个问题find解决scheme为此工作。 该文件太大,无法发送,所以解决scheme是部分发送给用户。 而不是使用readfile()函数,请使用readfile_chuncked()自定义函数:

 define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk // Read a file and display its content chunk by chunk function readfile_chunked($filename, $retbytes = TRUE) { $buffer = ''; $cnt = 0; $handle = fopen($filename, 'rb'); if ($handle === false) { return false; } while (!feof($handle)) { $buffer = fread($handle, CHUNK_SIZE); echo $buffer; ob_flush(); flush(); if ($retbytes) { $cnt += strlen($buffer); } } $status = fclose($handle); if ($retbytes && $status) { return $cnt; // return num. bytes delivered like readfile() does. } return $status; } 
    Interesting Posts