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

China☆狼群 提交于 2019-11-29 04:52:07

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.

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.

hdparm -i /dev/sdX

that's on linux, not sure on windows though. You could execute that via "system()"

Have a look at http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.hk.msdn.connection&tid=e41f0af2-2e76-4be6-9b7b-636e79ac0491&cat=zh_HK_3b03d742-993a-4f96-accd-1063c6bfd559&lang=zh&cr=HK&sloc=&p=1

Might be a way forward.

Also, when I ran a "dir" on the command prompt, it shows:

C:\Documents and Settings\Administrator>dir
 Volume in drive C has no label.
 Volume Serial Number is BC16-5D5F

Is that what you're looking for?

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" :-/

Run the following with shell_exec (test in command prompt if needed):

wmic path win32_physicalmedia get Tag,SerialNumber

DISKDRIVE doesn't get the actual serial number for my drive that shows plugged in through an IDE channel. the above seemed to get the actual serial numbers for all of my drives. Tag will also return you what type of drive it is which may be helpful for identifying different drives.

Example output:

SerialNumber     Tag
WD-WX55D33JQNZ4  \\.\PHYSICALDRIVE4
S1OKIJNH938475   \\.\PHYSICALDRIVE0
WD-CV44HJ5L765Y  \\.\PHYSICALDRIVE1
WD-WX41D65SD1UU  \\.\PHYSICALDRIVE2
WD-WXB1SD3OIJHG  \\.\PHYSICALDRIVE3
                 \\.\CDROM0

Do you want the hard drive from the server or a client? PHP runs on the server so getting it straight from the client doens't seem possible to me.

The manual suggest you can execute commands on you server: http://nl2.php.net/manual/en/ref.exec.php

Unfortunately I don't enough Unix to get you hdd serials.

Lenin

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.

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;

?>
Fery Wardiyanto

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!