- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为 webroot 以外的用户发送文件(附件)。我制作了强制下载脚本,它在 header 中发送文件并在流中输出。这很好用,直到我调用 readfile(也可以是标题设置),它输出一个文件,其中包含我的 html 源代码的一半(该特定页面的)。我做了 file_get_contents() 并且文件包含正确的字符串:“test”。这里有什么问题?
<?php
//The path where the files are stored
$file_path = "";
if(isset($_GET['file'])) {
$file_path = $_GET['file'];
}
else {
header('HTTP/1.0 404 Not Found');
die('File not found!');
}
$file_name = basename($_GET['file']);
//Make sure the file exists
if(!file_exists($file_path) && !is_file($file_name)) {
header('HTTP/1.0 404 Not Found');
die('File not found!');
}
//Initialize the actual file.
$file = $file_path;
//Notice: Remember to set fifo extension in php.ini
//Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.
$finfo = new finfo(FILEINFO_MIME);
$mime = "";
if(function_exists("finfo_open")) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $file);
}
// Provide a default type in case all else fails
else {
$mime = "application/octet-stream";
}
//Set the appropriate content-type and provide the content-length.
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-type: '.$mime);
header('Content-Disposition: attachment; filename='.$file_name);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.filesize($file));
//Print the image data
readfile($file);
exit;
?>
$file 名为 test.txt 并包含字符串“test”。 Mime/content-type 是正确的。
但是这个脚本的输出是 html 源。
最佳答案
来自readfile
文档,他们在 readfile
之前进行了两次调用(强制下载):ob_clean()
和 flush()
。我的假设是他们调用这些以确保 header 已发送并且客户了解有内容即将到来。
我建议在其中添加那些,它应该可以工作。
关于php - 强制下载脚本和读取文件 : Why readfile outputs my html page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264497/
为了测试并确保我已经把所有东西都放下了,以便以后可以开始扩展,我正在尝试创建一个文件句柄,将给定的字节缓冲区写入该文件句柄,然后读取部分该文件放入新的测试缓冲区。 我有代码: const size_t
这是我的问题: a method readLine that reads a line from the file specified in fileName and returns the
我被认为是一个菜鸟,我在互联网上搜索了几个小时来解决我的问题,但仍然没有运气。 我真的很想了解java,如果你能解释一些细节,我将非常感激。 问题出在这一行 ReadFile file = ReadF
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在编写一个程序,通过 USB 通过虚拟 com 端口与电机驱动器通信。我正在使用 windows.h file-io 与其通信(CreateFile、WriteFile、ReadFile)。我可以
我的问题是关于回调函数的工作方式。 const fs = require('fs'); let fileContents = 'initial value'; fs.readFile('file.tx
我正在尝试使用 readfile 读取一个文件,将其存储到一个宽数组中,然后将其写入另一个文件。问题是,当我将它们并排放置在 HxD 中时,一些字节是正确的(例如文本),但其他一切都完全不同。我也跑不
我正在通过将 COM 端口 RD 和 TD 引脚连接在一起来测试串行端口通信。在执行以下代码之前,COM 端口已被初始化。 CString cs_Send = "F: 5000000 Hz, SF:
我正在使用 Flash 播放器播放一些 mp3 文件。在 Firefox 上它可以正常加载它们,但在 IE 上却不能。当我转到 .mp3 文件的 url 时,它显示了 mp3 的源代码(而不是提供例如
在设置在线文件管理系统时,我遇到了障碍。 我正在尝试使用此 modified version of readfile 将文件推送到客户端: function readfile_chunked($fil
这很很奇怪。 我有一个脚本可以通过浏览器将本地 zip 文件发送给用户。到目前为止,该脚本运行良好,没有任何问题。今天我的同事通知我脚本正在发送零长度文件。 一些背景信息: 在脚本出错之前没有修改服务
在我的函数中,我正在下载一个将下载保存到日志文件的文件。每当我尝试下载我上传的 Excel 文件时,Excel 都会指出它已损坏。我的本地副本工作正常。这是我的 download.php:
是否可以在大小未知且大小不断增加的远程文件上使用 PHP readfile 函数?这是场景: 我正在开发一个脚本,它可以从第三方网站下载视频,同时将视频转码为 MP3 格式。这个 MP3 然后通过 r
我正在使用 PHP readfile 函数读取文件并像这样打印它:print readfile ('anyfile')。这可行,但文件的内容长度也会添加到字符串的末尾。为什么? 最佳答案 readfi
我正在尝试解决从 PHP 脚本下载“zip”文件时遇到的问题。似乎当我使用以下代码下载文件时,下载的文件在文件开头附加了一个额外的 0A09,导致 winzip 抛出损坏错误。 kill_sessi
我是 nodejs 和回调的新手。 所以我有这段代码,当通过 HTTP 发起对服务器的请求时,我在其中读取文件: var http = require("http"); var fs = requir
当使用 readfile() 时——在 Apache 上使用 PHP——文件是立即读入 Apache 的输出缓冲区并完成 PHP 脚本执行,还是 PHP 脚本执行等到客户端完成下载文件(或服务器)超时
我正在使用 php 脚本在必要的 javascript 计时器之后提供从我的网站的下载,此 php 脚本包含在内,导致下载。但是无论我尝试什么,下载的文件都是损坏的。谁能帮我指出我哪里出错了。 这是我
我正在使用下面的代码通过单击按钮下载 csv 文件。 JS: $(document).on("click", "#CSV", function () { var csv_value = $('
我有一个 php 代码,用于根据 URL 中的 file_id 下载文件。一切正常,但我在下载扩展名为 .doc 的文件时遇到问题 这是我的代码,我做错了什么? $file = mysql_fetch
我是一名优秀的程序员,十分优秀!