gpt4 book ai didi

php readfile 错误的字符集

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:10 26 4
gpt4 key购买 nike

我有一个 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 中下载文件后,我得到了这个。

enter image description here

我想,问题一定出在编码上……在这里输入图片描述

最佳答案

这对我来说也很糟糕。我在这里找到了解决方案: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com