适用于APP客户端的下载,直接使用即可
本人水平有限,不足之处请见谅,勿喷!望指出错误!安卓以测试,苹果未知!<?php <br />
/**<br>
* 手机访问此页面自动查找最新安装包并下载<br>
* author 394786996@qq.com<br>
* date 2016-4-14<br>
* 用法:<br>
* 将此文件放到跟目录下手机直接访问即可自动查找适合Android或Iphone的最新安装包,如需其他设备格式请修改 _ext() 方法<br>
* 也可将此链接生成二维码扫描<br>
* 电脑端下载时需要加想要下载的格式的后缀<br>
* 如:电脑:http://www.chaofan100.com/down.php?ext=apk , 手机:http://www.chaofan100.com/down.php<br>
* 安装包位置:/upload/app/<br>
* 安装包命名规则:******201604141111.apk 即 * = 随便,中文除外,数字为生成包的日期,判断最新便是判断的它<br>
*/<br>
header("Content-type:text/html; charset=utf-8");<br>
<br>
$exts = _ext();<br>
if (empty($exts)) {<br>
_jump('暂不支持您的系统!');<br>
} else {<br>
$file_sub_path = $_SERVER['DOCUMENT_ROOT']<br>
. DIRECTORY_SEPARATOR<br>
. 'upload'<br>
. DIRECTORY_SEPARATOR<br>
. 'app'<br>
. DIRECTORY_SEPARATOR;<br>
<br>
$dirs = _deepScanDir($file_sub_path);<br>
$files = $dirs['file'];<br>
<br>
$tmp = array();<br>
foreach ($files as $key => $value) {<br>
$ext = substr($value['filename'], strrpos($value['filename'], '.')+1);<br>
if( in_array($ext, $exts) )<br>
{<br>
$end = strrpos($value['filename'], '.');<br>
$start = $end - 12;<br>
$tmp[] = array('time' => substr($value['filename'], $start, 12), 'filepath' => $value['path'], 'filename' => $value['filename']);<br>
}<br>
}<br>
empty($tmp) && _jump('没有发现包!');<br>
$filearr = max($tmp);<br>
$file_path = $filearr['filepath'];<br>
// var_dump();die;<br>
// var_dump($tmp);die;<br>
$file_name = $filearr['filename'];<br>
<br>
//首先要判断给定的文件存在与否 <br>
if(!file_exists($file_path)) { <br>
_jump("没有该文件文件");<br>
} <br>
$fp = fopen($file_path,"r");<br>
$file_size = filesize($file_path);<br>
//下载文件需要用到的头 <br>
Header("Content-type: application/octet-stream");<br>
Header("Accept-Ranges: bytes");<br>
Header("Accept-Length: " . $file_size);<br>
Header("Content-Disposition: attachment; filename=" . $file_name);<br>
$buffer = 1024;<br>
$file_count = 0;<br>
//向浏览器返回数据 <br>
while(!feof($fp) && $file_count
$file_con = fread($fp,$buffer);<br>
$file_count += $buffer;<br>
echo $file_con;<br>
} <br>
fclose($fp);<br>
}<br>
<br>
function _jump($message = '跳转中...', $url = null) {<br>
$url = $url ? $url : $_SERVER['HTTP_HOST'];<br>
header("refresh:3; url=http://" . $url);<br>
print($message . '<br>三秒后自动跳转首页...');<br>
}<br>
<br>
// 获得适合的安装包后缀<br>
function _ext($agent = null) {<br>
$ext = array();<br>
$agent = $agent ? $agent : $_SERVER['HTTP_USER_AGENT'];<br>
$agent = strtolower($agent);<br>
if (strstr($agent, 'android')) {<br>
$ext = array('apk');<br>
} else if (strstr($agent, 'iphone')) {<br>
$ext = array('ipa','pxl','deb');<br>
} else if (isset($_GET['ext'])) {<br>
$ext = array($_GET['ext']);<br>
}<br>
return $ext;<br>
}<br>
<br>
// 获得目录和文件<br>
function _deepScanDir($dir) {<br>
//return $dir;<br>
$fileArr = array();<br>
$dirArr = array();<br>
$dir = rtrim($dir, '//');<br>
if(is_dir($dir)){<br>
$dirHandle = opendir($dir);<br>
while(false !== ($fileName = readdir($dirHandle))){<br>
$subFile = $dir . $fileName;<br>
if(is_file($subFile)){<br>
$fileArr[] = array('path' => $subFile,'filename' => $fileName);<br>
} elseif (is_dir($subFile) && str_replace('.', '', $fileName) != ''){<br>
$dirArr[] = array('path' => $subFile,'filename' => $fileName);<br>
}<br>
}<br>
closedir($dirHandle);<br>
}<br>
return array('dir' => $dirArr, 'file' => $fileArr);<br>
}<br>
<br>
?>
down.rar ( 1.8 KB 下载:4 次 )
AD:真正免费,域名+虚机+企业邮箱=0元