phpexcel 下载文件并保存在目录中



我正在使用带有codeignitor的phpexcel.以下代码要求仅下载。 我希望它还应该将文件保存在以下路径中

$filename= "kp.xlsx";$filePath = FCPATH。文件/application_download/".$filename;

if (file_exists($filePath)) {
             unlink($filePath);
            }
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $filename . '"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
          $objWriter->save('php://output');

如果我这样做$objWriter->保存($filePath(; 然后这将保存一个空白文件

与其直接保存到 php://output,不如将文件保存到服务器,然后使用 PHP readfile(( 函数将该文件流式传输到浏览器;但请确保您的 Web 服务器用户具有将文件写入该文件夹的权限。

$objWriter->save($filePath);
readfile($filePath);

如果你想自己命名,那么你可以按照我在$a中指定的那样指定

我想从调用此函数的位置使用文件名,因此我还将该名称连接到 excel 文件名,并将当前日期附加到文件名中

日志报告是一个文件夹,我的文件将被下载

self::$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, 'Excel2007');
        $a = 'Log' . self::$filename . date("Y-m-d") . '.xlsx';
        $objWriter->save(__DIR__ . '/LogReport/' . $a);

最新更新