Getting Serial Number of the Hard Drive Provided by the manufacturer through PHP

后端 未结 9 663
时光取名叫无心
时光取名叫无心 2020-12-17 03:36

Getting Serial Number of the Hard Drive Provided by the manufacturer through PHP : How can it be done? I want to store it in a file.

OS : windows 2000,XP,ME,Vista.

相关标签:
9条回答
  • 2020-12-17 03:53

    I can't tell you the answer, but I guess you'll have to look in the direction of extensions (maybe even writing one yourself). I doubt this is something PHP's core has.

    Edit: I forgot about the raw power of "exec" :-/

    0 讨论(0)
  • 2020-12-17 03:55

    You can use

        $hdserial =`wmic DISKDRIVE GET SerialNumber 2>&1` 
    

    or

        $hdserial =`wmic bios get serialnumber 2>&1` 
    

    Then you can echo it.

    Based on Patrick Daryll Glandien's hint, you can execute following on *nix based machines. $hdserial= hdparm -I /dev/hda

    hdparm -i /dev/sda returns lesser info. But as hdparm needs root access, it did not run with php for me.

    The '2>&1' part is used from the suggestion here.

    0 讨论(0)
  • 2020-12-17 03:57

    PHP itself has no way of accessing the hardware like that.

    You will have to either

    • use a command of your operating system and call it with system() or exec()
    • write an extension for PHP that will return you the information

    If you are on Linux and have the necessary privileges and configuration you can use $r = system("hdparm -I /dev/hda"); (replace hda with your hd) to get the serial number of a given hard drive.

    0 讨论(0)
  • 2020-12-17 04:00

    The following returns the disk serial number. Should work with multiple drives, you'll just get multiple results. Just run it with shell_exec.

    wmic DISKDRIVE GET SerialNumber
    

    wmic.exe is located in your windows system32 folder. And wmic does exist on WinXP, Ive used it there myself.

    My result on Vista:

    C:\Windows\System32>wmic DISKDRIVE GET SerialNumber
    SerialNumber
    20202020202054534241354c4*snip*
    

    I do not know if all harddrives provides the serial number to the OS.

    It seems the wmic command is only available on the professional versions of Windows XP, Windows Vista and Windows 7.

    0 讨论(0)
  • 2020-12-17 04:00

    Try this code it's working properly.

    <?php
       function GetVolumeLabel($drive) {
           // Try to grab the volume name
           if (preg_match('#Volume Serial Number is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) {
              $volname = ' ('.$m[1].')';
           } else {
               $volname = '';
           }
       return $volname;
    }
    
    $serial = str_replace("(","",str_replace(")","",GetVolumeLabel("c")));
    echo $serial;
    
    ?>
    
    0 讨论(0)
  • 2020-12-17 04:00

    On *nix based machine you can also use ls /dev/disk/by-id/ because hdparm need root permission (see Patrick Daryll G. answer).

    <?php
    exec($command.' 2>&1', $output);
    echo 'HDD: '.$output[0].'<br>';
    
    $outputs = explode('_', $outputs[0]);
    $outputs = end($outputs);
    echo 'HDD-SN: '.$output.'<br>';
    

    and you will get something like this

    HDD: ata-HGST_XXX1234567890XX_ABCD123456789X  // <connection>-<hdd_model>_<hdd_sn>
    HDD-SN: ABCD123456789X  // Your HDD Serial Number
    
    0 讨论(0)
提交回复
热议问题