Reading file and directory owner using PHP on the Windows

我是研究僧i 提交于 2019-12-23 06:08:28

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/56445688/reading-file-and-directory-owner-using-php-on-the-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!