
最近在做一个软件时,需要给它加一个在线更新的功能,与服务器上的最新软件进行版本比对,通过对比版本号来实现提示升级的功能。
常规的思路是在服务器MYSQL数据库中写入版本号,然后再通过PHP API输出。然而这样每次都要手动去修改版本号,这样太麻烦了。
于是在就在网上收集了一些资料,发现可以直接用php来读取服务器上最新版exe软件的版本号。
相关代码如下:
/**
* PHP 读取 exe\dll 文件版本号
*
* @auth @腾讯电脑管家(https://zhidao.baidu.com/question/246143241010222924.html)
* @param $filename 目标文件
* @return 读取到的版本号
*/
function getFileVersion($filename)
{
$fileversion = '';
$fpFile = @fopen($filename, "rb");
$strFileContent = @fread($fpFile, filesize($filename));
fclose($fpFile);
if($strFileContent)
{
$strTagBefore = 'F\0i\0l\0e\0V\0e\0r\0s\0i\0o\0n\0\0\0\0\0'; // 如果使用这行,读取的是 FileVersion
// $strTagBefore = 'P\0r\0o\0d\0u\0c\0t\0V\0e\0r\0s\0i\0o\0n\0\0'; // 如果使用这行,读取的是 ProductVersion
$strTagAfter = '\0\0';
if (preg_match("/$strTagBefore(.*?)$strTagAfter/", $strFileContent, $arrMatches))
{
if(count($arrMatches) == 2)
{
$fileversion = str_replace("\0", '', $arrMatches[1]);
}
}
}
return $fileversion;
}还有一种读取方法,缺点就是它只能支持 Windows 服务器:
/**
* PHP 读取 exe\dll 文件版本号(仅支持Windows服务器)
*
* @auth https://zhidao.baidu.com/question/475823802.html
* @param $filename 目标文件(必须是完整的路径)
* @return 读取到的版本号
*/
function getFileVersion($filename)
{
$fso = new COM('Scripting.FileSystemObject');
return $fso->GetFileVersion($filename);
} 









发布评论