Skip to content

Instantly share code, notes, and snippets.

@imbyc
Last active October 16, 2020 07:11
Show Gist options
  • Save imbyc/8ed5e6953cc751a8ced8664bf95d189c to your computer and use it in GitHub Desktop.
Save imbyc/8ed5e6953cc751a8ced8664bf95d189c to your computer and use it in GitHub Desktop.
[PHP 读取 exe\dll 文件版本号] 遇到大文件会有问题 #版本号 #exe #版本
<?php
/**
* PHP 读取 exe\dll 文件版本号
*
* @auth @腾讯电脑管家(https://zhidao.baidu.com/question/246143241010222924.html)
* @param $filename 目标文件
* @return string|string[]
*/
function getVersionFromFile($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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment