(PHP)将文件夹中的文件保存到计算机

(PHP)将文件夹中的文件保存到计算机,php,download,phpexcel,Php,Download,Phpexcel,我正在使用PHP(PHPExcel)处理所有excel文件。 目前我已经在一个文件夹中保存了一个“New Template.xlsx”。现在我希望用户只需点击一个按钮就可以下载“newtemplate.xlsx” 如何使用php或phpexcel(我也在使用Yii php框架)实现这一点 我不知道该怎么开始,我现在只有 $filePath = Yii::app()->basePath . '\\vendors\\Excel Template\\New Template.xlsx';

我正在使用PHP(PHPExcel)处理所有excel文件。 目前我已经在一个文件夹中保存了一个“New Template.xlsx”。现在我希望用户只需点击一个按钮就可以下载“newtemplate.xlsx”

如何使用php或phpexcel(我也在使用Yii php框架)实现这一点

我不知道该怎么开始,我现在只有

   $filePath = Yii::app()->basePath . '\\vendors\\Excel Template\\New Template.xlsx';
    Yii::import('application.vendors.PHPExcel', true);
    $objPHPExcel = PHPExcel_IOFactory::load($filePath);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
编辑

由于Miqi180的评论,我现在可以下载excel文件,但是由于文件扩展名/文件格式无效,我无法打开它

这是我的密码:

<?php

ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script

//$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$path = Yii::app()->basePath . '\\vendors\\Excel Template\\';
//$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', 'NewTemplate.xlsx'); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "xlsx":
        //header("Content-type: application/vnd.ms-excel");
        header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
        break;
        // add more headers for other content types here
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        break;
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
  • 阻止Yii发送自己的头和输出
  • xlsx
    文件发送适当的标题
  • 将文件保存到
    php://output
查看PHPExcel
examples
文件夹中的一些示例,如
01simple download xlsx.php


但是,除非您在加载模板和将模板发送给用户之间进行更改,否则不需要使用PHPExcel进行此操作。基本上,您需要执行一个php脚本,该脚本在加载另一个页面时启动,例如“download.php”,然后通过该脚本中的头命令发送文件

完整的工作代码示例取自:

或用于Excel2007及以上.xlsx文件

header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
编辑:

据此,Excel.xlsx文件的正确扩展MIME类型实际上是:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

嗨,谢谢你的回复。我面临另一个问题。我可以下载.xlsx格式的excel文件,但无法打开该文件。“Excel无法打开文件,因为文件格式或文件扩展名无效。请验证文件是否已损坏,以及文件扩展名是否与文件格式匹配”“记事本中的文件是什么样子?”?它里面有数据还是空的?它不是空的,它有一堆随机的涂鸦。我使用了您提供的“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”值。如果您使用的web服务器不支持开放XML格式的MIME类型,那么这也不会起作用。这可能是这里的问题。请检查您的设置,或联系您的服务提供商,
header("Content-type: application/vnd.ms-excel");
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet