Php 无法将文件发送到浏览器/用户

Php 无法将文件发送到浏览器/用户,php,file,header,Php,File,Header,我正在尝试向用户发送一个zip文件 $file_info = get_file_info($filepath_name); header("Content-Type: " . get_mime_by_extension($filepath_name)); header("Content-Length: " . $file_info["size"]); header_remove('Pragma'); header_remove('Cache-Control'); //header("Conten

我正在尝试向用户发送一个zip文件

$file_info = get_file_info($filepath_name);
header("Content-Type: " . get_mime_by_extension($filepath_name));
header("Content-Length: " . $file_info["size"]);
header_remove('Pragma');
header_remove('Cache-Control');
//header("Content-Type: application/force-download");
header('Content-Type: application/octet-stream');        
header('Content-Disposition: attachment; filename='.urlencode($filepath_name));
header('Location: file://'.$filepath_name);
readfile($filepath_name);
$filepath\u name
设置为“D:\dev2.local\storage\u users\1\export\u data\course\u 2357.zip”

readfile()
返回文件的正确大小,但文件仍无法下载


我尝试了所有标题设置的组合,但没有效果。

您的标题似乎有问题

您可以尝试以下方法:

ob_start();
//OR
ob_clean();
header("Content-Type: " . get_mime_by_extension($filepath_name));
/* Other code */

看起来你的头球有点问题

您可以尝试以下方法:

ob_start();
//OR
ob_clean();
header("Content-Type: " . get_mime_by_extension($filepath_name));
/* Other code */

我不知道这是否是您键入的顺序,但是:您是否尝试过输入:

$filepath_name =  "D:\\dev2.local\\storage_users\\1\\export_data\\course_2357.zip"
在代码顶部,或至少在
readfile()之前


正在等待您的更新。

我不知道您键入的顺序是否正确,但是:您是否尝试过输入:

$filepath_name =  "D:\\dev2.local\\storage_users\\1\\export_data\\course_2357.zip"
在代码顶部,或至少在
readfile()之前

正在等待更新。

标题('Location:file://'。$filepath\u name)看起来有问题。这是对函数的特殊使用,该函数将302重定向HTTP状态发送到浏览器,重定向到指定的URL

这里没有添加任何内容,我会删除它

ZIP存档的正确MIME类型是
application/ZIP
而不是
application/octet-stream
。发送正确的标题使浏览器更有可能在正确的程序中打开文件

URL编码文件的完整路径会导致文件名长度为69个字符。我认为这不太可能长到足以引发问题,但我们不妨通过发送基本名称“course_2357.zip”来简化它。我不认为URL编码文件名是必要的-这似乎源于Internet Explorer 8黑客让UTF-8字符工作,这在您的情况下是不需要的-但我把它留在原地,因为它没有做任何伤害

我使用PHP内置的
filesize
而不是
get\u file\u info
,因为我没有安装CodeIgniter。我想这就是这个函数的来源

以下基于您提供的上述内容的代码适用于我:

header("Content-Length: " . filesize($filepath_name));
header_remove('Pragma');
header_remove('Cache-Control');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.urlencode(basename($filepath_name)));
readfile($filepath_name);
header('位置:file://.$filepath\u name)看起来有问题。这是对函数的特殊使用,该函数将302重定向HTTP状态发送到浏览器,重定向到指定的URL

这里没有添加任何内容,我会删除它

ZIP存档的正确MIME类型是
application/ZIP
而不是
application/octet-stream
。发送正确的标题使浏览器更有可能在正确的程序中打开文件

URL编码文件的完整路径会导致文件名长度为69个字符。我认为这不太可能长到足以引发问题,但我们不妨通过发送基本名称“course_2357.zip”来简化它。我不认为URL编码文件名是必要的-这似乎源于Internet Explorer 8黑客让UTF-8字符工作,这在您的情况下是不需要的-但我把它留在原地,因为它没有做任何伤害

我使用PHP内置的
filesize
而不是
get\u file\u info
,因为我没有安装CodeIgniter。我想这就是这个函数的来源

以下基于您提供的上述内容的代码适用于我:

header("Content-Length: " . filesize($filepath_name));
header_remove('Pragma');
header_remove('Cache-Control');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.urlencode(basename($filepath_name)));
readfile($filepath_name);
发现的问题:

这个功能是通过AJAX调用调用的,这就是php不允许发送文件的原因

找到的解决方案: 必须将文件路径和名称从php所在的Ajax返回给js,并从js提供文件

编辑: 这是我使用的解决方法:

                                var hostname = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');

                            jQuery('<iframe>', {
                               src: hostname + value.return_zip,
                               id:  'iFrame_zip_download',
                               frameborder: 0,
                               scrolling: 'no'
                           }).appendTo('#export_import_status');
var hostname=window.location.protocol+“/”+window.location.hostname+(window.location.port?):“+window.location.port:”;
jQuery(“”{
src:hostname+value.return\u-zip,
id:“iFrame\u zip\u下载”,
帧边框:0,
滚动:“否”
}).appendTo(“#导出#导入#状态”);
其中value.return\u zip是服务器上文件的相对路径(/storage\u users/xx/xx/xx.zip)。然后我添加了主机名,结果很好,例如:。 iFrame隐藏在状态分区中。

发现问题:

这个功能是通过AJAX调用调用的,这就是php不允许发送文件的原因

找到的解决方案: 必须将文件路径和名称从php所在的Ajax返回给js,并从js提供文件

编辑: 这是我使用的解决方法:

                                var hostname = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');

                            jQuery('<iframe>', {
                               src: hostname + value.return_zip,
                               id:  'iFrame_zip_download',
                               frameborder: 0,
                               scrolling: 'no'
                           }).appendTo('#export_import_status');
var hostname=window.location.protocol+“/”+window.location.hostname+(window.location.port?):“+window.location.port:”;
jQuery(“”{
src:hostname+value.return\u-zip,
id:“iFrame\u zip\u下载”,
帧边框:0,
滚动:“否”
}).appendTo(“#导出#导入#状态”);
其中value.return\u zip是服务器上文件的相对路径(/storage\u users/xx/xx/xx.zip)。然后我添加了主机名,结果很好,例如:。
iFrame隐藏在status div中。

所有组合都不起作用:(我还将ob_end_flush()添加到该代码的结尾,仍然无法执行。)所有组合都不起作用:(我还将ob_end_flush()添加到该代码的结尾,仍然无法执行。)不会更改结果:(我添加了一些可能有用的建议。很好的解释,但仍然没有发生任何事情。这是我的返回标题:连接:保持活动内容处置:附件;文件名=课程\u 2357.zip内容长度:9235内容类型:应用程序/zip日期:2016年3月24日星期四12:01:47 GMT过期:1981年11月19日星期四08:52:00 GMT保持活动:时间out=5,max=94 P3P:CP=“IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS