问题
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.
回答1:
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.
回答2:
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()
orexec()
- 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.
回答3:
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?
回答4:
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" :-/
回答5:
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
回答6:
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.
回答7:
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.
回答8:
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;
?>
回答9:
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
来源:https://stackoverflow.com/questions/709108/getting-serial-number-of-the-hard-drive-provided-by-the-manufacturer-through-php