Php 无法下载带有示例脚本的文件

Php 无法下载带有示例脚本的文件,php,jquery,phpexcel,xlsx,Php,Jquery,Phpexcel,Xlsx,我想创建xlsx文件并将其作为附件发送到浏览器,以便用户立即下载。我确实复制粘贴了源代码。 但它不起作用。我曾经在require_中修复了路径,但问题出在其他地方 xlsx文件正确生成-当我在服务器上保存ot时,我可以打开它。它还被发送到浏览器-firebug的控制台在输出窗口中显示一些有趣的字符。标题也是正确的 但是没有另存为。。。将显示一个对话框。我做了一些基于谷歌搜索结果的基本检查-我在?>之后没有多余的空格 我的代码中唯一的区别是,我使用一些附加参数从jQuery的$.post函数调用p

我想创建
xlsx
文件并将其作为附件发送到浏览器,以便用户立即下载。我确实复制粘贴了源代码。 但它不起作用。我曾经在require_中修复了路径,但问题出在其他地方

xlsx
文件正确生成-当我在服务器上保存ot时,我可以打开它。它还被发送到浏览器-firebug的控制台在输出窗口中显示一些有趣的字符。标题也是正确的

但是没有另存为。。。将显示一个对话框。我做了一些基于谷歌搜索结果的基本检查-我在
?>
之后没有多余的空格

我的代码中唯一的区别是,我使用一些附加参数从jQuery的
$.post
函数调用php脚本

这可能是我无法下载此文件的原因吗

任何帮助都将不胜感激

  • PHP版本:5.4.20
  • PHPExcel版本:1.8.0
  • Server:Apache/2.4.6(Linux/SUSE)

此问题也发布在上。

出于安全原因,您不能通过ajax请求(如
$)下载文件


您可以使用在新窗口中打开的链接。

谢谢您的指导,我找到了一个解决方案。
<?php
// If user click the download link
if(isset($_GET['filename'])){
        // The directory of downloadable files
        // This directory should be unaccessible from web
        $file_dir="";


// Replace the slash and backslash character with empty string
        // The slash and backslash character can be dangerous
        $file_name=str_replace("/", "", $_GET['filename']);
        $file_name=str_replace("\\", "", $file_name);


// If the requested file is exist
        if(file_exists($file_dir.$file_name)){
                // Get the file size
                $file_size=filesize($file_dir.$file_name);
                // Open the file
                $fh=fopen($file_dir.$file_name, "r");


// Download speed in KB/s
                $speed=50;



// Initialize the range of bytes to be transferred
                $start=0;
                $end=$file_size-1;


// Check HTTP_RANGE variable
                if(isset($_SERVER['HTTP_RANGE']) &&
                        preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $arr)){

                        // Starting byte
                        $start=$arr[1];
                        if($arr[2]){
                                // Ending byte
                                $end=$arr[2];
                        }
                }


// Check if starting and ending byte is valid
                if($start>$end || $start>=$file_size){
                        header("HTTP/1.1 416 Requested Range Not Satisfiable");
                        header("Content-Length: 0");
                }
                else{
                        // For the first time download
                        if($start==0 && $end==$file_size){
                                // Send HTTP OK header
                                header("HTTP/1.1 200 OK");
                        } 
                        else{
                                // For resume download
                                // Send Partial Content header
                                header("HTTP/1.1 206 Partial Content");
                                // Send Content-Range header
                                header("Content-Range: bytes ".$start."-".$end."/".$file_size);
                        }


// Bytes left
                        $left=$end-$start+1;


// Send the other headers
                        header("Content-Type: application/octet-stream");
                        header("Accept-Ranges: bytes");
                        // Content length should be the bytes left
                        header("Content-Length: ".$left);
                        header("Content-Disposition: attachment; filename=".$file_name);

                        // Read file from the given starting bytes
                        fseek($fh, $start);
                        // Loop while there are bytes left
                        while($left>0){
                                // Bytes to be transferred
                                // according to the defined speed
                                $bytes=$speed*1024;
                                // Read file per size
                                echo fread($fh, $bytes);
                                // Flush the content to client
                                flush();
                                // Substract bytes left with the tranferred bytes
                                $left-=$bytes;
                                // Delay for 1 second
                                sleep(1);
                        }
                }


fclose($fh);
        }
        else
        {
                // If the requested file is not exist
                // Display error message
                echo "File not found!";

        }

        exit();
}
?>

<a href="download.php?filename=filename.pdf">Download.</a>