菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用PHP获取exe软件的版本号

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:php教程

PHP本身无法直接读取exe软件的版本号,但可以通过调用系统命令行工具来实现。以下是一个示例代码: ```php <?php function getExeVersion($exePath) { $output = shell_exec("wmic datafile where name='$exePath' get Version /value"); preg_match('/Version=(.*)/', $output, $matches); return isset($matches[1]) ? $matches[1] : null; } $exePath = "C:\\path\\to\\your\\program.exe"; $version = getExeVersion($exePath); echo "程序版本号: " . $version; ?> ``` 这段代码首先定义了一个名为`getExeVersion`的函数,该函数接受一个参数`exePath`,表示exe文件的路径。然后使用`shell_exec

最近在做一个桌面客户端软件,顺手给它加上了在线更新的功能:与服务端的最新软件进行版本比对,如果本地版本低于服务端版本就提示升级。

常规思路是在服务端放个配置文件,里面写入版本号,然后再通过 api 输出。然而这样每次都要手动去修改版本号,难免会手抖将版本号输错。好在,咱们的 php 是万能的因此可以直接用 php 来读取服务器上最新版 exe 的版本号,方便省事可靠!

相关代码如下:

functiongetFileVersion($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 服务器。留以备用。

functiongetFileVersion($filename){$fso=newCOM('Scripting.FileSystemObject');return$fso->GetFileVersion($filename);}


相关文章

    无相关信息
评论
建站知识
建站知识
使用技巧
调试安装
运营推广