5

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...

Yes, I want the serial number of the hard drive of the Server.

Or can it be done through Adobe AIR? Or can it be done through a C program on Windows?

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

Is this number : BC16-5d5f unique for a hard drive? How is it different from the manufacturer given serial number?

wmic DISKDRIVE GET SerialNumber

Displays only the following text on my Vista Machine:

SerialNumber

On my XP machine, the command is unrecognized.

simplfuzz
  • 12,479
  • 24
  • 84
  • 137
  • The reason you don't get an answer is because your specific harddrive either does not provide the OS with this information or is damaged. – OIS Sep 25 '13 at 22:07

9 Answers9

8

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.

OIS
  • 9,833
  • 3
  • 32
  • 41
  • My research concluded that wmic is on Windows XP Professional but not Windows XP Home. Did you have it on Home? I am looking for a way to get some info and I'd like to use WMIC. – Dorothy Sep 20 '11 at 21:58
  • @coding_idiot It does on my copy of Windows 7 Home Premium (64 bit). Just open a CLI window and type the command above. – OIS Sep 25 '13 at 21:55
5

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.

Patrick Glandien
  • 7,791
  • 5
  • 41
  • 47
  • To avoid root access, we also have `ls /dev/disk/by-id/` see [my answer](http://stackoverflow.com/a/26751810/881743) ;) – Fery W Nov 05 '14 at 07:34
2
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?

Andrei Serdeliuc ॐ
  • 5,828
  • 5
  • 39
  • 66
1

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
Dustin G
  • 46
  • 1
  • 3
1

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

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
0

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.

Community
  • 1
  • 1
Lenin
  • 570
  • 16
  • 36
  • This answer got accepted for the wmic part [here](http://stackoverflow.com/questions/13797030/how-to-get-hard-disk-serial-number-with-php-on-localhost#comment18977745_13797030). – Lenin Dec 10 '12 at 12:18
  • This is the same as my answer, just 3 years later... And the reason it's not accepted is the question poster either doesn't have the correct wmic command (cause he doesn't have professional) or the hdd does not give the OS the serial number. – OIS Sep 25 '13 at 22:03
  • @OIS Do you mean to say I just duplicated your answer? – Lenin Sep 29 '13 at 06:55
  • Yes. Your bios example is wrong, and your unix example is outside the scope. – OIS Sep 29 '13 at 09:24
  • There's no obvious reasons you can claim so. I take that as your opinion only. :) – Lenin Oct 07 '13 at 13:37
0

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

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
Community
  • 1
  • 1
Fery W
  • 1,402
  • 1
  • 15
  • 28
0

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.

MrHus
  • 32,888
  • 6
  • 31
  • 31