tags:

views:

128

answers:

4

Is getting the version of a exe possible with php? I'd like to print the version of a file that can be downloaded...

Windows exe and php is running on linux server

+1  A: 

No, there's no easy, automatic way. You can certainly open the file and read it and if you know where to look you may be able to figure out the version. But there's no standard for storing version numbers in applications, so you'd have to know where to look for every sort application you have.

Scott Saunders
+3  A: 

I am assuming you mean the Version information that can be stored in Windows executables, and pops up in the properties dialog for such a file in Windows Explorer.

This information seems to be stored in the VS_VERSION_INFO block of an executable (see for example this question). I don't know any tool that extracts this information in a simple way, not even on Windows itself.

There seem to be several ways to get hold of this information via the Windows API (See a Perl example here) but I can't see any approach that works "from scratch" by just parsing the executable.

If you dig around a bit, you might be able to find a file format description that explains how to read the VS_VERSION_INFO information from an EXE file. Be prepared for a lot of work to get this to work reliably, though.

Be prepared to invest a lot of time and effort if you want to do this.

Pekka
Windows exe and php is running on linux server
Kai
+4  A: 

On a win32 machine you can use the COM extension and FileSystemObject.GetFileVersion() method to retrieve the version info. e.g.

$path = getenv('SystemRoot').'\\NOTEPAD.EXE';
$fso = new COM('Scripting.FileSystemObject');
echo $path, ' : ', $fso->GetFileVersion($path);

prints (on my machine) C:\WINDOWS\NOTEPAD.EXE : 5.1.2600.5512

VolkerK
sorry, no win32 machine
Kai
A: 

I wanted the same thing, so I coded this: It returns FALSE, if it can't get the version info, or an ARRAY of four elements with file version fields (numbers, which are separated by .) It works only for 32-bit PE files (as I had no need for other formats).

function GetFileVersion($FileName) {

$handle=fopen($FileName,'rb');
if (!$handle) return FALSE;
$Header=fread ($handle,64);
if (substr($Header,0,2)!='MZ') return FALSE;
$PEOffset=unpack("V",substr($Header,60,4));
if ($PEOffset[1]<64) return FALSE;
fseek($handle,$PEOffset[1],SEEK_SET);
$Header=fread ($handle,24);
if (substr($Header,0,2)!='PE') return FALSE;
$Machine=unpack("v",substr($Header,4,2));
if ($Machine[1]!=332) return FALSE;
$NoSections=unpack("v",substr($Header,6,2));
$OptHdrSize=unpack("v",substr($Header,20,2));
fseek($handle,$OptHdrSize[1],SEEK_CUR);
$ResFound=FALSE;
for ($x=0;x<$NoSections[1];$x++) {
    $SecHdr=fread($handle,40);
    if (substr($SecHdr,0,5)=='.rsrc') {         //resource section
        $ResFound=TRUE;
        break;
    }
}
if (!$ResFound) return FALSE;
$InfoVirt=unpack("V",substr($SecHdr,12,4));
$InfoSize=unpack("V",substr($SecHdr,16,4));
$InfoOff=unpack("V",substr($SecHdr,20,4));
fseek($handle,$InfoOff[1],SEEK_SET);
$Info=fread($handle,$InfoSize[1]);
$NumDirs=unpack("v",substr($Info,14,2));
$InfoFound=FALSE;
for ($x=0;$x<$NumDirs[1];$x++) {
    $Type=unpack("V",substr($Info,($x*8)+16,4));
    if($Type[1]==16) {                          //FILEINFO resource
        $InfoFound=TRUE;
        $SubOff=unpack("V",substr($Info,($x*8)+20,4));
        break;
    }
}
if (!$InfoFound) return FALSE;
$SubOff[1]&=0x7fffffff;
$InfoOff=unpack("V",substr($Info,$SubOff[1]+20,4)); //offset of first FILEINFO
$InfoOff[1]&=0x7fffffff;
$InfoOff=unpack("V",substr($Info,$InfoOff[1]+20,4));    //offset to data
$DataOff=unpack("V",substr($Info,$InfoOff[1],4));
$DataSize=unpack("V",substr($Info,$InfoOff[1]+4,4));
$CodePage=unpack("V",substr($Info,$InfoOff[1]+8,4));
$DataOff[1]-=$InfoVirt[1];
$Version=unpack("v4",substr($Info,$DataOff[1]+48,8));
$x=$Version[2];
$Version[2]=$Version[1];
$Version[1]=$x;
$x=$Version[4];
$Version[4]=$Version[3];
$Version[3]=$x;
return $Version;

}

Toni