我在E盘下有个文件,路径是E:\\Apache2\\htdocs\\WULS\downtest\\Document (2).txt内容是:HELLO HELLOPHP中代码
function doS($_file)
{
$file = fopen($_file,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($_file));
Header("Content-Disposition: attachment; filename=" . basename($_file));
echo fread($file,filesize($_file));
fclose($file);
}
doS("E:\\Apache2\\htdocs\\WULS\downtest\\Document (2).txt");
我点了DOWNLOAD按钮后..调用这个方法..文件下载下来了.但下来下来的TXT文件中多了PHP文件中的<HEAD>部分.我HTML的代码下面是TXT中内容可能有点乱<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
HELLO HELLO
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FUYO Web U/D System Login</title>
<link rel="stylesheet" type="text/css" href="../CSS/Reset.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/Style.css"/>
<script type="text/javascript" src="../JS/mootools121.js"></script>
<script type="text/javascript" src="../JS/Manage.js"></script>
</head><body id="doc">
<div><h1>FUYO Web U/L System</h1></div>
<div>
<ul>
<li>Adiminstrator:</li>
<li>admin</li>
<li>2009/12/8</li>
<li><a href="Manage.php?action=logout">LogOut</a></li>
</ul>
</div>
<div id="Usermanage">
<form id="form1" name="form1" action="#" method="post">
<input type="hidden" name="Uid" id="Uid" value=""/>
<input type="button" value="+ ADD" name="add" onclick="ShowLayout('addlayout');"/>
<input type="button" value="- DEL" name="del" onclick="Buttonaction('del');"/>
<input type="button" value="# Modify" name="modify" onclick="Buttonaction('fillform');"/>
<table cellpadding="0" cellspacing="1" id="UserTable">
<tbody>
<tr><th style="width:20px">Opt</th>
<th style="width:30px">ID</th>
<th style="width:50px">UserName</th>
<th style="width:50px">Role</th></tr>
<tr><td><input type ='checkbox' id='chk_0' name='chk_0' onclick ="OptCheckBox('chk_0');" value='00001'/></td><td>00001</td><td>admin</td><td>Adiminstrator</td></tr><tr><td><input type ='checkbox' id='chk_1' name='chk_1' onclick ="OptCheckBox('chk_1');" value='00002'/></td><td>00002</td><td>power</td><td>Power User</td></tr><tr><td><input type ='checkbox' id='chk_2' name='chk_2' onclick ="OptCheckBox('chk_2');" value='00004'/></td><td>00004</td><td>bizee</td><td>Power User</td></tr><tr><td><input type ='checkbox' id='chk_3' name='chk_3' onclick ="OptCheckBox('chk_3');" value='20002'/></td><td>20002</td><td>hly</td><td>Power User</td></tr> </tbody>
</table>
</form>
</div> <div id="addlayout" name="addlayout" style="display:none;">
<form id="form2" action="Manage.php?action=add" method="post">
<div><div>Add Account</div><div><a href="#">X</a></div></div>
<div>
<table>
<tr><td>User ID</td><td><input type="text" value="" maxlength="5" name="userid"/></td></tr>
<tr><td>User Name</td><td><input type="text" value="" maxlength="10" name="username"/></td></tr>
<tr><td>PassWord</td><td><input type="password" value="" maxlength="8" name="password"/></td></tr>
<tr><td>Role</td><td><select id="sel_role" name="sel_role">
<option selected="true" value="">---- Opt ----</option>
<option value="01">Adiminstrator</option>
<option value="02">Power User</option>
</select></td></tr>
</table>
<div><input type="submit" value="submit" name="add"/></div>
</div>
</form>
</div>
<div id="addUDfilelayout" name="addUDfilelayout">
<div><div>New Update File</div></div>
<form id ="frm_uf" name="frm_uf" enctype="multipart/form-data" action="" method="post">
<span>Note: support files type *.txt,*.rar,*.zip,*.pdf,*.doc,*.xls,*.ppt and image file</span><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"/>
<input type ="hidden" name="filesize" id="filesize" value=""/>
<input type="file" name="ufile" id="ufile" /><br/>
<span style="color:#33CC33;"></span><br/>
<input type="button" name="upload" value="upload" onclick="UpLoad();" />
</form>
<form id="form5" name="form5" action="" method="post">
<fieldset>
<legend>Download conditions set</legend>
<input type="radio" checked="checked" value="00" align="left" name="limited" id="limited1"/>Unlimited<br/>
<input type="radio" value="01" align="left" name="limited" id="limited2"/>Time effectively restricted
<input type="text" name="days" id="days" value="0" style="text-align:right; width:30px" maxlength="2"/>days
Or <input type="text" name="hours" id="hours" value="0" style="text-align:right; width:30px" maxlength="2"/>hours Or <input type="text" name="minutes" id="minutes" value="0" style="text-align:right; width:30px" maxlength="2"/>minutes<br/>
<input type="radio" value="02" align="left" name="limited" id="limited3"/>Limited number of times <input type="text" name="times" id="times" value="0" style="text-align:right; width:30px" maxlength="3"/>times<br/>
<input type="radio" value="03" align="left" name="limited" id="limited4"/>Password Restrictions
<input type="password" name="pw" id="pw" value="" style="width:80px" maxlength="8"/><span style="color:red;">8-bit password</span><br/>
<input type="submit" value="OK"/><input type="button" value="cancel"/>
</fieldset>
</form>
</div> <div id="Filemanage">
<form name="form4" id="form4" action="#" method="post">
<input type="hidden" name="MId" id="MId" value=""/>
<input type="button" value="All"/>
<input type="button" value="None"/>
<input type="button" value="ReSet"/>
<input type="button" value="Delete File"/>
<input type="button" value="DownLoad" name="down" onclick="Buttonaction('download');"/>
<table cellpadding="0" cellspacing="1" id="FileTable">
<tbody>
<tr><th>Opt</th><th>ID</th><th>FilePath</th><th>FileName</th><th>DownLoadURL</th></tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
function doS($_file)
{
$file = fopen($_file,"r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($_file));
Header("Content-Disposition: attachment; filename=" . basename($_file));
echo fread($file,filesize($_file));
fclose($file);
}
doS("E:\\Apache2\\htdocs\\WULS\downtest\\Document (2).txt");
我点了DOWNLOAD按钮后..调用这个方法..文件下载下来了.但下来下来的TXT文件中多了PHP文件中的<HEAD>部分.我HTML的代码下面是TXT中内容可能有点乱<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
HELLO HELLO
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FUYO Web U/D System Login</title>
<link rel="stylesheet" type="text/css" href="../CSS/Reset.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/Style.css"/>
<script type="text/javascript" src="../JS/mootools121.js"></script>
<script type="text/javascript" src="../JS/Manage.js"></script>
</head><body id="doc">
<div><h1>FUYO Web U/L System</h1></div>
<div>
<ul>
<li>Adiminstrator:</li>
<li>admin</li>
<li>2009/12/8</li>
<li><a href="Manage.php?action=logout">LogOut</a></li>
</ul>
</div>
<div id="Usermanage">
<form id="form1" name="form1" action="#" method="post">
<input type="hidden" name="Uid" id="Uid" value=""/>
<input type="button" value="+ ADD" name="add" onclick="ShowLayout('addlayout');"/>
<input type="button" value="- DEL" name="del" onclick="Buttonaction('del');"/>
<input type="button" value="# Modify" name="modify" onclick="Buttonaction('fillform');"/>
<table cellpadding="0" cellspacing="1" id="UserTable">
<tbody>
<tr><th style="width:20px">Opt</th>
<th style="width:30px">ID</th>
<th style="width:50px">UserName</th>
<th style="width:50px">Role</th></tr>
<tr><td><input type ='checkbox' id='chk_0' name='chk_0' onclick ="OptCheckBox('chk_0');" value='00001'/></td><td>00001</td><td>admin</td><td>Adiminstrator</td></tr><tr><td><input type ='checkbox' id='chk_1' name='chk_1' onclick ="OptCheckBox('chk_1');" value='00002'/></td><td>00002</td><td>power</td><td>Power User</td></tr><tr><td><input type ='checkbox' id='chk_2' name='chk_2' onclick ="OptCheckBox('chk_2');" value='00004'/></td><td>00004</td><td>bizee</td><td>Power User</td></tr><tr><td><input type ='checkbox' id='chk_3' name='chk_3' onclick ="OptCheckBox('chk_3');" value='20002'/></td><td>20002</td><td>hly</td><td>Power User</td></tr> </tbody>
</table>
</form>
</div> <div id="addlayout" name="addlayout" style="display:none;">
<form id="form2" action="Manage.php?action=add" method="post">
<div><div>Add Account</div><div><a href="#">X</a></div></div>
<div>
<table>
<tr><td>User ID</td><td><input type="text" value="" maxlength="5" name="userid"/></td></tr>
<tr><td>User Name</td><td><input type="text" value="" maxlength="10" name="username"/></td></tr>
<tr><td>PassWord</td><td><input type="password" value="" maxlength="8" name="password"/></td></tr>
<tr><td>Role</td><td><select id="sel_role" name="sel_role">
<option selected="true" value="">---- Opt ----</option>
<option value="01">Adiminstrator</option>
<option value="02">Power User</option>
</select></td></tr>
</table>
<div><input type="submit" value="submit" name="add"/></div>
</div>
</form>
</div>
<div id="addUDfilelayout" name="addUDfilelayout">
<div><div>New Update File</div></div>
<form id ="frm_uf" name="frm_uf" enctype="multipart/form-data" action="" method="post">
<span>Note: support files type *.txt,*.rar,*.zip,*.pdf,*.doc,*.xls,*.ppt and image file</span><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"/>
<input type ="hidden" name="filesize" id="filesize" value=""/>
<input type="file" name="ufile" id="ufile" /><br/>
<span style="color:#33CC33;"></span><br/>
<input type="button" name="upload" value="upload" onclick="UpLoad();" />
</form>
<form id="form5" name="form5" action="" method="post">
<fieldset>
<legend>Download conditions set</legend>
<input type="radio" checked="checked" value="00" align="left" name="limited" id="limited1"/>Unlimited<br/>
<input type="radio" value="01" align="left" name="limited" id="limited2"/>Time effectively restricted
<input type="text" name="days" id="days" value="0" style="text-align:right; width:30px" maxlength="2"/>days
Or <input type="text" name="hours" id="hours" value="0" style="text-align:right; width:30px" maxlength="2"/>hours Or <input type="text" name="minutes" id="minutes" value="0" style="text-align:right; width:30px" maxlength="2"/>minutes<br/>
<input type="radio" value="02" align="left" name="limited" id="limited3"/>Limited number of times <input type="text" name="times" id="times" value="0" style="text-align:right; width:30px" maxlength="3"/>times<br/>
<input type="radio" value="03" align="left" name="limited" id="limited4"/>Password Restrictions
<input type="password" name="pw" id="pw" value="" style="width:80px" maxlength="8"/><span style="color:red;">8-bit password</span><br/>
<input type="submit" value="OK"/><input type="button" value="cancel"/>
</fieldset>
</form>
</div> <div id="Filemanage">
<form name="form4" id="form4" action="#" method="post">
<input type="hidden" name="MId" id="MId" value=""/>
<input type="button" value="All"/>
<input type="button" value="None"/>
<input type="button" value="ReSet"/>
<input type="button" value="Delete File"/>
<input type="button" value="DownLoad" name="down" onclick="Buttonaction('download');"/>
<table cellpadding="0" cellspacing="1" id="FileTable">
<tbody>
<tr><th>Opt</th><th>ID</th><th>FilePath</th><th>FileName</th><th>DownLoadURL</th></tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
$filename = pathinfo($fullname, 2);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"$filename\"");
Header("Content-Length: $nFileSize");
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
Header("Expires: 0");
echo file_get_contents($fullname);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"$filename\"");
Header("Content-Length: $nFileSize");
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
echo file_get_contents($fullname);
{
$filename = pathinfo($fullname, 2);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"$filename\"");
Header("Content-Length: $nFileSize");
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
echo file_get_contents($fullname);
}
HELLO HELLO
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FUYO Web U/D System Login</title>
<link rel="stylesheet" type="text/css" href="../CSS/Reset.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/Style.css"/>
<script type="text/javascript" src="../JS/mootools121.js"></script>
<script type="text/javascript" src="../JS/Manage.js"></script>
.....................
......................
还是有HEAD部分和HTML部分呢
HELLO HELLO
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FUYO Web U/D System Login </title>
<link rel="stylesheet" type="text/css" href="../CSS/Reset.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/Style.css"/>
<script type="text/javascript" src="../JS/mootools121.js"> </script>
<script type="text/javascript" src="../JS/Manage.js"> </script>
.....................
...................... 红色部分都是不应该有的内容才对
你下载的部分,是由js控制的
<script type="text/javascript" src="../JS/mootools121.js"> </script>
<script type="text/javascript" src="../JS/Manage.js"> </script> 所以有可能问题不在php部分,在js部分.
<?php
require("../Comm/Comm.php");
require("download.class.php");
require("../Comm/BusinessRules/DBC_Manage.php");
$user = $_SESSION["UserInfo"];
$db = new DBC_Comm();
$udb = new DBC_Manage();
$mesg="";
$uplflag = false;if (isset($_REQUEST["action"]))
{
switch($_REQUEST["action"])
{
case "download":
function doS($fullname)
{
$filename = pathinfo($fullname, 2);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"".$filename."\"");
Header("Content-Length:". $nFileSize);
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
Header("Expires: 0");
echo file_get_contents($fullname);
}
doS("E:\\Apache2\\htdocs\\WULS\downtest\\Document (2).txt");
break;
default:
break;
}
}
?><html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FUYO Web U/D System Login</title>
<link rel="stylesheet" type="text/css" href="../CSS/Reset.css"/>
<link rel="stylesheet" type="text/css" href="../CSS/Style.css"/>
<script type="text/javascript" src="../JS/mootools121.js"></script>
<script type="text/javascript" src="../JS/Manage.js"></script>
</head><body id="doc">
<div><h1>FUYO Web U/L System</h1></div>
<div>
<ul>
<li></li>
<li></li>
<li></li>
<li><a href="Manage.php?action=logout">LogOut</a></li>
</ul>
</div>
<div id="Usermanage">
<form id="form1" name="form1" action="#" method="post">
<input type="hidden" name="Uid" id="Uid" value=""/>
<input type="button" value="+ ADD" name="add" onclick="ShowLayout('addlayout');"/>
<input type="button" value="- DEL" name="del" onclick="Buttonaction('del');"/>
<input type="button" value="# Modify" name="modify" onclick="Buttonaction('fillform');"/>
<table cellpadding="0" cellspacing="1" id="UserTable">
<tbody>
<tr><th style="width:20px">Opt</th>
<th style="width:30px">ID</th>
<th style="width:50px">UserName</th>
<th style="width:50px">Role</th></tr>
</tbody>
</table>
</form>
</div> <div id="addlayout" name="addlayout" style="display:none;">
<form id="form2" action="Manage.php?action=add" method="post">
<div><div>Add Account</div><div><a href="#">X</a></div></div>
<div>
<table>
<tr><td>User ID</td><td><input type="text" value="" maxlength="5" name="userid"/></td></tr>
<tr><td>User Name</td><td><input type="text" value="" maxlength="10" name="username"/></td></tr>
<tr><td>PassWord</td><td><input type="password" value="" maxlength="8" name="password"/></td></tr>
<tr><td>Role</td><td><select id="sel_role" name="sel_role">
<option selected="true" value="">---- Opt ----</option>
<option value="01">Adiminstrator</option>
<option value="02">Power User</option>
</select></td></tr>
</table>
<div><input type="submit" value="submit" name="add"/></div>
</div>
</form>
</div> <div id="Filemanage">
<form name="form4" id="form4" action="#" method="post">
<input type="hidden" name="MId" id="MId" value=""/>
<input type="button" value="All"/>
<input type="button" value="None"/>
<input type="button" value="ReSet"/>
<input type="button" value="Delete File"/>
<input type="button" value="DownLoad" name="down" onclick="Buttonaction('download');"/>
这里点击下载按钮后通过JS调用最上面的PHP代码 <table cellpadding="0" cellspacing="1" id="FileTable">
<tbody>
<tr><th>Opt</th><th>ID</th><th>FilePath</th><th>FileName</th><th>DownLoadURL</th></tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
require("../Comm/Comm.php");
require("download.class.php");
require("../Comm/BusinessRules/DBC_Manage.php");
$user = $_SESSION["UserInfo"];
$db = new DBC_Comm();
$udb = new DBC_Manage();
$mesg="";
$uplflag = false; if (isset($_REQUEST["action"]))
{
switch($_REQUEST["action"])
{
case "download":
function doS($fullname)
{
$filename = pathinfo($fullname, 2);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"".$filename."\"");
Header("Content-Length:". $nFileSize);
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
Header("Expires: 0");
echo file_get_contents($fullname);
}
doS("E:\\Apache2\\htdocs\\WULS\downtest\\Document (2).txt");
break;
default:
break;
}
}
exit();
?>
这部分代码放到最上面
注意我加了一个exit();的这一行.
因为你是下载文件,
下载文件的php里,除了文件内容以外,不能有任何输出.包括html,js.
header之前也不能有任何输出.
<input type="button" value="DownLoad" name="down" onclick="Buttonaction('download');"/>
代码调用到了
function doS($fullname)
{
$filename = pathinfo($fullname, 2);
$nFileSize = filesize($fullname);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=\"".$filename."\"");
Header("Content-Length:". $nFileSize);
Header('Accept-Ranges: bytes');
Header("Cache-control: private");
Header('Pragma: private');
Header("Expires: 0");
echo file_get_contents($fullname);
}但是下载下来后的TXT文件会把内容是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
HELLO HELLO
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
.......
...省略掉的HTML代码....
.....
被下载的TXT文件中内容是HELLO HELLO,红色部分应该是PHP中的代码..为什么下载下来后会写进去???