如何使用 PHP 检测计算机上的所有硬盘驱动器


How to detect all hard drives on a machine using PHP

如何使用PHP来检测计算机上的所有驱动器?此检测将包括:

  • 硬盘
  • 笔式驱动器
  • 硬盘

如何在多个平台上做到这一点,例如Linux和Windows?

如果你使用的是 Linux,你可以使用 PHP 的 exec() 函数来执行以下 2 个命令:

fdisk -l

其中列出了磁盘和分区如下:

Disk /dev/xvda1: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvda1 doesn't contain a valid partition table
Disk /dev/xvda2: 365.0 GB, 365041287168 bytes
255 heads, 63 sectors/track, 44380 cylinders, total 712971264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvda2 doesn't contain a valid partition table
Disk /dev/xvda3: 939 MB, 939524096 bytes
255 heads, 63 sectors/track, 114 cylinders, total 1835008 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/xvda3 doesn't contain a valid partition table

和这个命令:

mount

其中列出了活动挂载点,包括交换。输出如下:

/dev/xvda1 on / type ext4 (rw)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/xvda2 on /mnt type ext3 (rw)

通过解析这些驱动器的输出,您可以获取所有驱动器的信息。还有更多命令(例如 cat /proc/partitions ) 在 Linux 中可用,您可以探索替代方案。


对于Windows来说,它更复杂。您可以使用(所需的管理权限):

fsutil fsinfo drives

返回(信息量不是很大):

Drives: C:' D:' E:' F:'

详细的驱动器信息可通过以下方式获得:

diskpart

然后发布:

list volume

然后将显示以下输出:

Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
----------  ---  -----------  -----  ----------  -------  ---------  --------
Volume 0     E                       DVD-ROM         0 B  No Media
Volume 1         System Rese  NTFS   Partition    100 MB  Healthy    System
Volume 2     C   System       NTFS   Partition     99 GB  Healthy    Boot
Volume 3     F   Data (local  NTFS   Partition    365 GB  Healthy

但是由于 PHP 无法执行命令并在输入中键入多个命令,因此可能无法直接获取信息。请考虑使用批处理程序输出所需的信息,并使用 PHP 调用该批处理程序。