- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 php 代码,用于根据 URL 中的 file_id 下载文件。一切正常,但我在下载扩展名为 .doc 的文件时遇到问题
这是我的代码,我做错了什么?
$file = mysql_fetch_array(mysql_query("SELECT * FROM ".sqlprefix."files WHERE id = '".$_GET['id']."'")); $tmp = explode(".",$file['url']); $tmp = $tmp[计数($tmp)-1]; //$tmp = "文档";
switch ($tmp) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "docx": $ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
case "doc": $ctype="application/msword"; break;
case "csv":
case "xls":
case "xlsx": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "tif":
case "tiff": $ctype="image/tiff"; break;
case "psd": $ctype="image/psd"; break;
case "bmp": $ctype="image/bmp"; break;
case "ico": $ctype="image/vnd.microsoft.icon"; break;
default: $ctype="application/force-download";
}
$baseName = basename($file['url_del']);
$fileSize = filesize($file['url_del']);
$url = $file['url_del'];
// $ctype = "application/msword";
// $baseName = "File name with spaces and diacritic ěščěšč.doc"
// $fileSize = "214016"
// $url = "./files/File name with spaces and diacritic ěščěšč.doc";
header('Content-Description: File Transfer');
header('Content-Type: '.$ctype);
header('Content-Disposition: attachment; filename='.$baseName);
header('Expires: 0');
header('Pragma: public');
header('Content-Length: '.$fileSize);
readfile($url);
exit;
在 word 中下载文件后,我得到了这个。
我想,问题一定出在编码上……在这里输入图片描述
最佳答案
这对我来说也很糟糕。我在这里找到了解决方案:PHP readfile() causing corrupt file downloads
尝试将此行放在 readfile() 之前:
//clean all levels of output buffering
while (ob_get_level()) {
ob_end_clean();
}
关于php readfile 错误的字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205855/
为了测试并确保我已经把所有东西都放下了,以便以后可以开始扩展,我正在尝试创建一个文件句柄,将给定的字节缓冲区写入该文件句柄,然后读取部分该文件放入新的测试缓冲区。 我有代码: 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
我是一名优秀的程序员,十分优秀!