Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Version of exe via PHP

Tags:

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

like image 223
Kai Avatar asked Jan 08 '10 17:01

Kai


People also ask

How to check PHP version in Windows?

Check PHP Version 1 Open the Command Prompt or Terminal. If you have PHP installed locally, you can use the Command Prompt or Terminal to check the version. ... 2 Enter the command to check the PHP version. When you run the command, the installed version of PHP will be displayed. 3 Fix the version not appearing in Windows. See More....

What version of PHP do I have installed on my Mac?

Mac - Open Terminal from the Utilities folder. Linux - Open Terminal from the dash, or by pressing Ctrl + Alt + T. Enter the command to check the PHP version. When you run the command, the installed version of PHP will be displayed. Fix the version not appearing in Windows.

How do I know if PHP is installed locally or remotely?

Local PHP Version. Open the Command Prompt or Terminal. If you have PHP installed locally, you can use the Command Prompt or Terminal to check the version. This also works if you use SSH to create a remote connection to your server via the command line.

What is phpversion () in PHP?

If a string argument is provided for extension parameter, phpversion () returns the version of that extension, or false if there is no version information associated or the extension isn't enabled. extension is nullable now. // is, the newer a PHP version is used. It's defined as used in the above


2 Answers

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')
        {
            $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;
}
like image 82
Toni Avatar answered Oct 30 '22 13:10

Toni


I recently moved our hosting from Windows to Linux. This was quite easy to do with VBScript given the Microsoft objects, but on Linux and PHP I couldn't find anything. We wrote this function in PHP to scan the .exe file and extract the "Product Version" that a VB.Net application had in it. You can change the $key to be whatever string you can find that has the version info and a null terminator.

Note that this scans the files in 64k chunks looking for the $key string. If you have a large .exe, it may take a few seconds. My .exe is 52k so it is nearly instant. If you have a larger exe, you can change the scan.

<?php

function get_product_version($file_name)
{
   $key = "P\x00r\x00o\x00d\x00u\x00c\x00t\x00V\x00e\x00r\x00s\x00i\x00o\x00n\x00\x00\x00";
   $fptr = fopen($file_name, "rb");
   $data = "";
   while (!feof($fptr))
   {
      $data .= fread($fptr, 65536);
      if (strpos($data, $key)!==FALSE)
         break;
      $data = substr($data, strlen($data)-strlen($key));
   }
   fclose($fptr);
   if (strpos($data, $key)===FALSE)
      return "";
   $pos = strpos($data, $key)+strlen($key);
   $version = "";
   for ($i=$pos; $data[$i]!="\x00"; $i+=2)
      $version .= $data[$i];
   return $version;
}

echo get_product_version("/path_to_file/foo.exe");
?>
like image 42
Scott Bartgis Avatar answered Oct 30 '22 13:10

Scott Bartgis