PHP 自定义提示下载文件函数

来源:百度知道 编辑:UC知道 时间:2024/05/13 02:21:35
自定义自动下载文件函数

以下函数调用的时候,有的时候保存下来的文件还有当前页面的内容(不成功),有的时候却能成功的提示下载只含有$constr的文件(成功),错在哪个地方 ,如何让函数永远只保存$constr里的值,而不包涵当前页面的内容啊?,回答满意还加分
function down_file($constr,$filename)
{
ob_end_clean();
header("Content-Encoding: none");
header("Content-Type: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream'));

header("Content-Disposition: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ')."filename=".$filename);

header("Content-Length: ".strlen($constr));
header("Pragma: no-cache");

header("Expires: 0");
echo $constr;
$e=ob_get_contents();
ob_end_clean();
}

我怀疑你这个地方错了。
header("Content-Type: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream'));

改一下
$pathinfo = pathinfo($filename);
$filetype = $pathinfo['extension']; //取得扩展名
header('Content-type: '.$filetype);

希望能对你有帮助。如果解决了你的问题,记得加分。