0

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.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78

1 Answers1

2

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

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • It seems that if there are any multibyte characters in the pathname, it does not work properly. Does anybody know a way around it? – Ham Dong Kyun Jan 22 '21 at 01:20
  • Yes, well, you need to convert filenames to UTF-8 to can display properly. But comparing and including files you can do only with direct path. It is a bit complicated but it's only way. Look at this repo I made for encodings: https://github.com/CreativForm/PHP-Solutions/blob/master/class.encoding.php – Ivijan Stefan Stipić Jan 25 '21 at 09:27