实现文件下载是通过在一个resource.php点击之后响应userdownfile.php实现的。
其中实现文件下载的方法是:
function download_by_path($path_name){
ob_end_clean();
$hfile = fopen($path_name, "rb") or die("Can not find file: $path_name\n");
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: ".filesize($path_name));
Header("Content-Disposition: attachment; filename=\"$path_name\"");
while (!feof($hfile)) {
echo fread($hfile, 1024);
}
fclose($hfile);
}页面提供下载的按钮是:
<form name="form" action="userDownmsg.php" method="post">
<input type="hidden" name="count" value="1" />
<input type="submit" name="download" value="downlaod"/> <?php echo $fileArray[1];?>
</form>我怀疑是不是提供下载功能的PHP文件某个方法出错了才导致出现乱码。
PHP文件的函数:
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
function getCity($ip = '')
{
if($ip == ''){
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
$ip=json_decode(file_get_contents($url),true);
$data = $ip;
}else{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
}
return $data;
}
$address=" ";
$ip=getIP();
$City1=getCity(getIP());
$City2=getCity();
if($City1!=false){
$address=$City1['country']." ".$City1['country_id']." ".$City1['area']." ".$City1['area_id']." ".$City1['region']." ".$City1['region_id'].
" ".$City1['city']." ".$City1['city_id']." ".$City1['isp']." ".$City1['isp_id']."\r\n";
}else{
$address=$City2['country']." ".$City2['province']." ".$City2['city']." ".$City2['district']." ".$City2['isp']." ".$City2['type']." ".$City2['desc'].$ip."\r\n"; }
$date=getTime();
//$ip = $_SERVER["REMOTE_ADDR"];
$userMsg= $ip;
$browse=getbrowseMsg();
$filename=getfilename();
$contentA=$filename." ".$date."\r\n".$userMsg."\r\n".$browse.$address."\r\n";
$compile_dir = "./downfile.txt";
$file = fopen($compile_dir,"a");
fwrite($file,$contentA);
fclose($file);
乱码是这样出现的,点击下载之后浏览器加载了很久,然后跳转到提供文件下载的PHP文件,文件显示了一堆乱码。GIF89a,一堆黑色的菱形里面有问号然后一堆@@
其中实现文件下载的方法是:
function download_by_path($path_name){
ob_end_clean();
$hfile = fopen($path_name, "rb") or die("Can not find file: $path_name\n");
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: ".filesize($path_name));
Header("Content-Disposition: attachment; filename=\"$path_name\"");
while (!feof($hfile)) {
echo fread($hfile, 1024);
}
fclose($hfile);
}页面提供下载的按钮是:
<form name="form" action="userDownmsg.php" method="post">
<input type="hidden" name="count" value="1" />
<input type="submit" name="download" value="downlaod"/> <?php echo $fileArray[1];?>
</form>我怀疑是不是提供下载功能的PHP文件某个方法出错了才导致出现乱码。
PHP文件的函数:
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
function getCity($ip = '')
{
if($ip == ''){
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
$ip=json_decode(file_get_contents($url),true);
$data = $ip;
}else{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
}
return $data;
}
$address=" ";
$ip=getIP();
$City1=getCity(getIP());
$City2=getCity();
if($City1!=false){
$address=$City1['country']." ".$City1['country_id']." ".$City1['area']." ".$City1['area_id']." ".$City1['region']." ".$City1['region_id'].
" ".$City1['city']." ".$City1['city_id']." ".$City1['isp']." ".$City1['isp_id']."\r\n";
}else{
$address=$City2['country']." ".$City2['province']." ".$City2['city']." ".$City2['district']." ".$City2['isp']." ".$City2['type']." ".$City2['desc'].$ip."\r\n"; }
$date=getTime();
//$ip = $_SERVER["REMOTE_ADDR"];
$userMsg= $ip;
$browse=getbrowseMsg();
$filename=getfilename();
$contentA=$filename." ".$date."\r\n".$userMsg."\r\n".$browse.$address."\r\n";
$compile_dir = "./downfile.txt";
$file = fopen($compile_dir,"a");
fwrite($file,$contentA);
fclose($file);
乱码是这样出现的,点击下载之后浏览器加载了很久,然后跳转到提供文件下载的PHP文件,文件显示了一堆乱码。GIF89a,一堆黑色的菱形里面有问号然后一堆@@
download_by_path($path_name);
function download_by_path($path_name){
ob_end_clean();
$hfile = fopen($path_name, "rb") or die("Can not find file: $path_name\n");
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: ".filesize($path_name));
Header("Content-Disposition: attachment; filename=\"$path_name\"");
while (!feof($hfile)) { echo fread($hfile, 1024);
}
fclose($hfile);
}
然后访问download.php 看看是否可以下载。