我想通过点击来实现下载:<a href="down.php?id=<? echo $id?>" target="_blank">下载</a>down.php全代码如下(网上查来的):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
</head>
<body>
<?php
$id=$_GET['id'];
$SQL="select * from filedown where id=".$id;
$rs=mssql_query($SQL,$sqlLink);
list($id,$title,$puttime,$fileurl,$filesrc)=mssql_fetch_row($rs);
$filedownUrl = $fileurl.$filesrc;
if(strpos($filedownUrl, 'http://') !== false || strpos($filedownUrl, 'ftp://') !== false) {
header("Location: $filedownUrl");
exit();
}else {
$filename = basename($filedownUrl); //取文件名
$pathinfo = pathinfo($filename);
$filetype = $pathinfo['extension']; //取得扩展名
$filesize = filesize($filedownUrl);
if(ob_get_length() !== false) @ob_end_clean(); //清除以前的缓冲
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-length: '.$filesize);
//readfile($filedownUrl);
$fp = @fopen($filedownUrl,"rb");
while(!feof($fp)) {
$buffer= fread($fp,8192);
echo $buffer;
}
@flush();
@ob_flush();
}
?>
</body>
</html>但是运行后总出现Warning: Cannot modify header information - headers already sent by (output started at 错误网上查了很多方法都不行,包括output_buffering = On 等等如果把fileurl改成本地的,比如C:\Downloads\1.txt,在本地运行就可以实现功能,一个文件跳出来提示保存
请大家帮忙看看,谢谢
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
</head>
<body>
<?php
$id=$_GET['id'];
$SQL="select * from filedown where id=".$id;
$rs=mssql_query($SQL,$sqlLink);
list($id,$title,$puttime,$fileurl,$filesrc)=mssql_fetch_row($rs);
$filedownUrl = $fileurl.$filesrc;
if(strpos($filedownUrl, 'http://') !== false || strpos($filedownUrl, 'ftp://') !== false) {
header("Location: $filedownUrl");
exit();
}else {
$filename = basename($filedownUrl); //取文件名
$pathinfo = pathinfo($filename);
$filetype = $pathinfo['extension']; //取得扩展名
$filesize = filesize($filedownUrl);
if(ob_get_length() !== false) @ob_end_clean(); //清除以前的缓冲
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-type: '.$filetype);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-length: '.$filesize);
//readfile($filedownUrl);
$fp = @fopen($filedownUrl,"rb");
while(!feof($fp)) {
$buffer= fread($fp,8192);
echo $buffer;
}
@flush();
@ob_flush();
}
?>
</body>
</html>但是运行后总出现Warning: Cannot modify header information - headers already sent by (output started at 错误网上查了很多方法都不行,包括output_buffering = On 等等如果把fileurl改成本地的,比如C:\Downloads\1.txt,在本地运行就可以实现功能,一个文件跳出来提示保存
请大家帮忙看看,谢谢
解决方案 »
- php获取url参数
- 有没有在线的php代码格式化工具?
- 插入mysql时,字符串中有引号,该如何处理?
- AJAX求解
- 浏览器缓存,Web服务器缓存,反向代理缓存。这些缓存都在什么情况下使用合适?
- 查询服务器数据库时间老是慢八小时
- LAMP环境,能否访问另外一台服务器中的MS SQL Server或Oracle数据库的数据?
- 求助:WINXP+IIS+PHP+MYSQL+PHPMYADMIN+Zend Optimizer的完整安装配置调试文档!
- 一个比较简单的问题!绝对给分!
- 一句 SQL 插入多条数据,能把所有lastInsertId都取回来吗?
- 用php如何获得字符串中图片的地址
- phpmail发gmail邮件出错
把
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title> </title>
</head>
<body>
这些去掉·
一列filesrc是文件名:1.txt,
filedowUrl变量是将两者连接起来
去掉的话那我网页标题没了,这个我想保留的
1在页面顶部的php标签中加入ob_start();2在返回的信息下面加入ob_end_flush();这样就可以屏蔽错误信息的现实了http://hi.baidu.com/apxsoft/blog/item/c0640dd3e66697093bf3cf77.html
header("Content-Type: application/force-download"); //设置类型
header("Content-Disposition: attachment; filename=".basename($filename)); //设置位置信息
加个这个可以了,谢谢大家