Search code examples
phpwindowsshellfile-ownership

Reading file and directory owner using PHP on the Windows


I have an intranet project based on the PHP and xampp server on the Windows. That intranet project have database with table where I store scanned disc drive informations of files and folders. I using simple windows command dir "$directory_path" /Q /T:C to get informations.

After that I use regex to match and parse that info to separate date, time, permission and dir name like on this example:

EXAMPLE REGEX FOR DIRECTORY PARSING

It's similar like this example: Get file owner in Windows using PHP

This working fine but sometimes file owner names are realy long and was stripped by command line. In that case I can't read full name and functionality is broken.

Now I searching for another solution.

My PHP have instaled also php_com_dotnet.dll extension and I using COM() class in some part of code.

My main question is:

-Can I use COM() class to get real file informations and avoid shell commands for the file search and listings and how?

-Is there some another extension, shell command or something 3rd what I can use to get file/folder owners?

NOTE: I need this only for the reading and indexing in the database, don't need to change permissions or ownership.


Solution

  • I find simple solution for this using PHP class COM() and the documentation of the IADsSecurityUtility::GetSecurityDescriptor method.

    The code in PHP looks like this:

    $path = 'D:\Some File\Some Another File\document.doc'; // File or dir path
    $su = new COM("ADsSecurityUtility"); // Call interface
    $securityInfo = $su->GetSecurityDescriptor($path, 1, 1); // Call method
    echo $securityInfo->owner; // Get file owner
    

    That's all folks.

    NOTE

    COM functions are only available for the Windows version of PHP.

    .Net support requires PHP 5 and the .Net runtime.

    As of PHP 5.3.15 / 5.4.5, this extension requires php_com_dotnet.dll to be enabled inside of php.ini in order to use these functions. Previous versions of PHP enabled these extensions by default.

    You are responsible for installing support for the various COM objects