.php中的代碼 php
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
$smarty->caching = true;
}
$times=times($smarty->template_dir);
$smarty->assign('times', $times);
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'price')
{
download($smarty->template_dir,'meiyiShop.apk');
// GetIP();
}
$smarty->display('download.dwt');
function GetIP(){
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
function download($address,$name){
header("Content-type:text/html;charset=utf-8");
// $file_name="cookie.jpg";
$file_name=$name;
//用以解決中文不能顯示出來的問題
$file_name=iconv("utf-8","gb2312",$file_name);
$file_sub_path=$address;
$file_path=$file_sub_path."/download/".$file_name;
//首先要判斷給定的文件存在與否
if(!file_exists($file_path)){
echo "沒有該文件文件";
return ;
}
$fp=fopen($file_path,"r");
$file_size=filesize($file_path);
//下載文件須要用到的頭
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition: attachment; filename=".$file_name);
$buffer=1024;
$file_count=0;
//向瀏覽器返回數據
while(!feof($fp) && $file_count<$file_size){
$file_con=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_con;
}
fclose($fp);
add_ip($address);
add_times($address);
}
function add_times($address){
$file_sub_path=$address;
$file_path=$file_sub_path."/download/"."times.txt";
$homepage = file_get_contents($file_path);
$data=(int)($homepage);
$data++;
File_put_contents($file_path,$data);
}
function times($address){
$file_sub_path=$address;
$file_path=$file_sub_path."/download/"."times.txt";
$homepage = file_get_contents($file_path);
return $data=(int)($homepage);
}
function add_ip($address){
$ip= GetIP();
date_default_timezone_set('Asia/Shanghai');//設置時區爲中國時間
$time=date("Y/m/d H:i:s"); //用data()取得時間
$data="ip地址:".$ip."--------------------下載時間:".$time;
$file_sub_path=$address;
$file_path=$file_sub_path."/download/"."record.txt";
File_put_contents($file_path,$data."\r\n",FILE_APPEND);
}
?>
html
.dwt 文件 瀏覽器
<a href="download.php?act=price" >下載</a>
<br>
<a>下載{$times}次</a>
cookie
和三個文件,一個是下載文件,一個是記錄次數的文件,一個是記錄IP的文件 app