我想要实现的是根据用户的操作系统触发自动文件下载。使用下面的代码我得到空字符串(虽然应该有文件的url)和页面刷新循环。
e.g。
Windows
var downloadTimeout = setTimeout(function () {
window.location = 'there supposed to be url to file but its blank';
}, 500);
有人可以指导我做错了吗?
操作系统检测:
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
);
foreach ($os_array as $regex => $value)
if (preg_match($regex, $user_agent))
$os_platform = $value;
return $os_platform;
}
if( substr(getOS(), 0, 6) === 'Windows' ){
$file = 'https://path-to-file.exe';
}
else if( substr(getOS(), 0, 2) === 'Mac' ){
$file = 'https://path-to-file.dmg';
}
else if( substr(getOS(), 0, 4) === 'Linux' ){
$file = 'https://path.air';
}
else{
}
?>
自动下载触发器:
var downloadTimeout = setTimeout(function () {
window.location = '<?php echo $file; ?>';
}, 500);
答案 0 :(得分:-1)
我找到了一个对我有用的解决方法。没什么好看的,只是一个想法
getOS()工作正常并回应我的操作系统;基于$ os_array中的选项,所以我将链接放到我的文件而不是操作系统名称,而不是使用getOS而不是$ file变量。丑陋,但至少有效