The LogicalDrives class is a small utility class that works only on Windows platforms and allows you to query the logical disks that are declared on your system.
It relies on the Windows Management Instrumentation interface (WMI) to retrieve logical drive information.
Documentation about WMI can be found here : https://msdn.microsoft.com/fr-fr/library/aa394582(v=vs.85).aspx.
You can also get some information about the kind of data held in a LogicalDrive object here : https://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx (an object of class LogicalDrive maps directly to the WMI class Win32_LogicalDisk).
Additionally, you will find a more generic WMI access implementation in PHP here : http://www.phpclasses.org/package/10001-PHP-Provides-access-to-Windows-WMI.html
Before retrieving drive information, you need to have an instance of the LogicalDrives class :
include ( 'LogicalDrives.phpclass' ) ;
$ld = new LogicalDrives ( ) ;
The constructor takes no parameter.
You can then call one of the methods implemented by the LogicalDrives class, such as GetAssignedDrives(), which retrieves the list of currently assigned logical drives :
$assigned_drives = $ld -> GetAssignedDrives ( ) ;
the method will return an array of strings containing the drive letters that are currently assigned on your system, followed by a semicolon. You can go further and use them for indexing your LogicalDrives instance and get access to individual drive information, which will be presented as an object of class LogicalDrive ; the following example will display the volume name (or label) for each drive letter that has been assigned to your system :
foreach ( $assigned_drives as $letter )
{
$drive = $ld [ $letter ] ; // You can access a LogicalDrives object as an array
echo "Drive $letter ({$drive -> VolumeName})\n" ;
}
The LogicalDrives class is the main interface to access logical drive information. Since it implements the ArrayAccess interface, it can be accessed using either :
- an integer index ; integer indexes are limited to the range 0..25, for the drive letters 'A' through 'Z'
- a string index ; in this case, this is a drive letter optionally followed by a semicolon, such as in : "A", "B:", "C", "D:", etc.
The class also implements the Iterator interface, meaning that you can retrieve individual drive information using foreach() constructs.
The constructor takes no parameter and immediately retrieves information about all the logical disks that are defined on your system.
public function GetAssignedDrives ( )
Returns an array of strings representing the drive letters that are currently assigned on your system.
Drive letters are systematically in uppercase and followed by a semicolon.
public function GetNextAVailableDrive ( )
Returns the next available drive letter.
Drive letters are systematically in uppercase and followed by a semicolon.
public function GetUnassignedDrives ( )
Returns an array of strings representing the drive letters that are currently unassigned on your system (and therefore free for later use).
Drive letters are systematically in uppercase and followed by a semicolon.
The LogicalDrive class encapsulates a Wmi WIN32_LogicalDisk object for a given logical drive and implements all the properties that can be found in such an object (see https://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx for more information about this Wmi class, and about all the constants that are defined in the LogicalDrive class).
You will find additional methods and constants that are described below :
public function GetDriveType ( )
Returns the drive type as a string ; it can be any of the following values, depending on the drive :
- "Unknown" : Unknown drive type
- "No root directory" : Drive is assigned, but does not contain any root directory (and maybe, no filesystem at all)
- "Removable disk" : Drive is a removable disk, such as a USB device
- "Local disk" : Local disk, physically hardwired to your system
- "Network drive" : Drive is mapped to a network resource.
- "Compact disk" : Drive is a compact disk reader and/or writer
- "Ram disk" : Drive is a ram disk
- "Unknown drive type 0xab" : Drive type unknown. "ab" gives the hex code of the drive type.
This method is designed only to provide a human-readable string describing the drive type. To programmatically determine the actual drive type, it is far better to use the DriveType property and compare it against one of the LogicalDrive::DRIVE_TYPE_* constants.
public function GetNetworkName ( ) ;
Returns an associative array containing the following entries :
- 'fullname' : the full network name (eg, "\\myhost\myresource"1)
- 'host' : the server name (eg, "myhost")
- 'resource' : the resource name (eg, "myresource")
$status = $drive -> IsCompactDisk ( ) ;
$status = $drive -> IsLocalDisk ( ) ;
$status = $drive -> IsNetworkDrive ( ) ;
$status = $drive -> IsRamDisk ( ) ;
$status = $drive -> IsRemovableDisk ( ) ;
These functions check that the drive has the specified characteristic : compact disk, local to the system, network-mapped, ram disk or removable disk.
public static function NormalizeDriveLetter ( $letter )
This function is mainly used internally, but has been made public in case of... It returns a "normalized" drive letter corresponding to the supplied input.
The return value is always an uppercase letter followed by a semicolon.
The LogicalDrive class implements the ArrayAccess and Iterator interfaces, so that you can use for() or foreach() constructs to loop through the individual properties currently exposed by the underlying WMI object.
All the constants described below are documented here : https://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx
- Drive access types (Access property) :
- DRIVE_ACCESS_UNKNOWN
- DRIVE_ACCESS_READ
- DRIVE_ACCESS_WRITE
- DRIVE_ACCESS_READ_WRITE
- DRIVE_ACCESS_WRITE_ONCE
- Drive availabilities (Availability property) :
- DRIVE_AVAILABILITY_OTHER
- DRIVE_AVAILABILITY_UNKNOWN
- DRIVE_AVAILABILITY_RUNNING
- DRIVE_AVAILABILITY_WARNING
- DRIVE_AVAILABILITY_TESTING
- DRIVE_AVAILABILITY_NOT_APPLICABLE
- DRIVE_AVAILABILITY_POWER_OFF
- DRIVE_AVAILABILITY_OFFLINE
- DRIVE_AVAILABILITY_OFF_DUTY
- DRIVE_AVAILABILITY_DEGRADED
- DRIVE_AVAILABILITY_NOT_INSTALLED
- DRIVE_AVAILABILITY_INSTALL_ERROR
- DRIVE_AVAILABILITY_POWER_SAVE_UNKNOWN_STATE
- DRIVE_AVAILABILITY_POWER_SAVE_LOW_POWER
- DRIVE_AVAILABILITY_POWER_SAVE_STANDBY
- DRIVE_AVAILABILITY_POWER_CYCLE
- DRIVE_AVAILABILITY_POWER_SAVE_WARNING
- Drive types (DriveType property) :
- DRIVE_TYPE_UNKNOWN
- DRIVE_TYPE_NO_ROOT_DIRECTORY
- DRIVE_TYPE_REMOVABLE_DISK
- DRIVE_TYPE_LOCAL_DISK
- DRIVE_TYPE_NETWORK_DRIVE
- DRIVE_TYPE_COMPACT_DISK
- DRIVE_TYPE_RAM_DISK
- Media types (MediaType property) :
- MEDIA_TYPE_UNKNOWN
- MEDIA_TYPE_FLOPPY_5_1x2_512bs
- MEDIA_TYPE_FLOPPY_5_1x44_512bs
- MEDIA_TYPE_FLOPPY_2x88_512bs
- MEDIA_TYPE_FLOPPY_3_20x8_512bs
- MEDIA_TYPE_FLOPPY_3_720_512bs
- MEDIA_TYPE_FLOPPY_5_360_512bs
- MEDIA_TYPE_FLOPPY_5_320_512bs
- MEDIA_TYPE_FLOPPY_5_320_1024bs
- MEDIA_TYPE_FLOPPY_5_180_512bs
- MEDIA_TYPE_FLOPPY_5_160_512bs
- MEDIA_TYPE_REMOVABLE_MEDIA
- MEDIA_TYPE_FIXED_HARD_DISK
- MEDIA_TYPE_FLOPPY_3_120M_512bs
- MEDIA_TYPE_FLOPPY_3_640_512bs
- MEDIA_TYPE_FLOPPY_5_640_512bs
- MEDIA_TYPE_FLOPPY_5_7230_512bs
- MEDIA_TYPE_FLOPPY_3_1x2_512bs
- MEDIA_TYPE_FLOPPY_3_1x23_1024bs
- MEDIA_TYPE_FLOPPY_5_1x23_1024bs
- MEDIA_TYPE_FLOPPY_3_128M_512bs
- MEDIA_TYPE_FLOPPY_3_230M_512bs
- MEDIA_TYPE_FLOPPY_8_256_128bs
- Power management capabilities (PowerManagementCapabilities property, which can be any combination of the flags listed below) :
- PMC_UNKNOWN
- PMC_NOT_SUPPORTED
- PMC_DISABLED
- PMC_ENABLED
- PMC_AUTO_POWER_SAVING
- PMC_POWER_STATE_SETTABLE
- PMC_POWER_CYCLING_SUPPORTED
- PMC_TIMED_POWER_ON_SUPPORTED
- Logical device state (StatusInfo property) :
- STATUS_OTHER
- STATUS_UNKNOWN
- STATUS_ENABLED
- STATUS_DISABLED
- STATUS_NOT_APPLICABLE