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