【问题标题】:phpexcel download file and save in directoryphpexcel下载文件并保存在目录中
【发布时间】:2016-02-01 10:15:59
【问题描述】:

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

$filename="kp.xlsx"; $filePath = FCPATH."files/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->save($filePath);那么这将保存一个空白文件

【问题讨论】:

    标签: save phpexcel


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果你想自己给名字,那么你可以像我在$a中指定的那样指定

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

      LogReport 是我的文件将被下载的文件夹

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 2017-02-13
        • 2022-12-03
        相关资源
        最近更新 更多