Skip to content

Instantly share code, notes, and snippets.

@lancegliser
Last active September 25, 2017 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancegliser/e32262264326668e26f6f81be03684f4 to your computer and use it in GitHub Desktop.
Save lancegliser/e32262264326668e26f6f81be03684f4 to your computer and use it in GitHub Desktop.
A better alternative available through COM objects to the madness of using exec('net stat')
<?php
/**
* Creates a temporary network share drive connection to the share
* using windows COM object for the executing PHP identity as a different user
*
* @param string $letter
* @param string $path
* @param string $user
* @param string $password
* @return boolean
*/
function map_windows_shared_drive($letter, $path, $user, $password)
{
try {
$network_com = new COM("WScript.Network");
$network_com->MapNetworkDrive(
"{$letter}:",
$path,
FALSE,
$user,
$password
);
} catch (Exception $exception) {
show_error($exception->getMessage());
}
return TRUE;
}
/**
* Closes a network drive stored by the executing PHP identity
*
* @param object $network
* @param string $letter
* @return NULL
*/
function disconnect_windows_shared_drive($network, $letter)
{
$network->RemoveNetworkDrive("{$letter}:", TRUE, FALSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment