3

I'm using WAMP server in windows 7 and trying to use posix function in php but it's not working and giving fatal error:undefined function. Now i want to know, how i can install it on windows.

If it's not available for windows how would like to know how to get file owner name by id on Windows, and where can I read about implementing similar posix features on Windows.

kabanus
  • 24,623
  • 6
  • 41
  • 74

2 Answers2

4

Obviously, since Windows proper is not a POSIX operating system, you cannot use PHP's POSIX support in a standard WAMP stack.

The Windows equivalent is PHP's Windows Only Extensions. This includes support for using .NET classes, which allows you to do most things that any other Windows application can do.

For example, in order to find out the owner of a file, you might use System.IO.File.GetAccessControl as described in the answers to this question. You are likely to find existing questions and answers to address most other needs you might have.

(You may prefer to consider switching to a LAMP stack, on a virtual machine if necessary.)


Nitpicker's corner:

Windows 10 provides beta POSIX support via the Windows Subsystem for Linux, and some versions of Windows Server support Interix, another POSIX-based subsystem, but to the best of my knowledge no standard WAMP stack makes either of these directly available from PHP.

In any case, if you are using OS-specific extensions in the first place it is almost always going to be preferable to use the functionality native to the actual host operating system in order to avoid unnecessary complications. For example, retrieving the WSL owner ID for a file on an NTFS partition potentially loses information, since Linux only supports 32-bit IDs, very different to Windows SIDs.

Disclaimer: I have no idea what WSL actually does in such cases; it is entirely possible that in some scenarios this would not present an actual problem. My point is merely that having to find out how this works in order to make sure it will meet your needs is an unnecessary complication compared to either using the functionality native to Windows, or running your service on a genuine Linux host if that is your preference.

Community
  • 1
  • 1
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • 1
    [WSL File System Support](https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/) offers an in-depth look on how the WSL virtual file system is implemented. Since *"Windows file systems differ substantially from Linux file systems"* I very much agree, that a native implementation is the best option. Posted the link for the curious mind only. – IInspectable Dec 28 '16 at 11:24
1

This is not as simple as in Linux, and posix is not trivially used in Windows (as the plethora of comments below suggest). There is a POSIX 1.0 implementation invoked by the posix subsystem, though this was deprecated since Windows 8. Another option is SFU (Service for Unix) which is also deprecated (removed?) since Win 8, though a new Windows 10 bash! is going to replace it - I'm not familiar enough to know how easily you can use it but it may bring a change. Finally there are 3'd party application you can use to mimic the UNIX environment like Cygwin, but I think that's a bit much to expect your server to use them from an optimization point of view.

In my opinion your best bet is invoking Native Windows commands through C++.Your question is a bit too vague in my opinion so I'll only address only your example. Basically what you need are:

The following example uses the GetSecurityInfo and LookupAccountSid functions to find and print the name of the owner of a file

This is taken from the msdn docs which you need to get acquainted with, and contains many native Windows APIs. In there you will find a long example of what you need (for the example - this is a full implementation using the above functions, so just copy paste and compile.

EDIT - PHP

Regarding PHP, there is no clean solution for getting the user ID. I would suggest calling a small C++ exe implemented as above if you need this, through the exec function or some such.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • i did not understand reason of your negative mark. I have clearly mention that if this function its not for windows so please tell me another function. I'm new in php i couldn't find better way that's why i posted question here to get suggestion from professional and Your answer is use less for me because its in c++ i want to know in php. – Muhammad Hamza Nisar Dec 26 '16 at 09:25
  • 1
    'how i can install it on windows' can be interpreted by 'how can i install posix on windows' so he answered you about it and the rest of you question is unclear, i mean too much wide – Frankich Dec 26 '16 at 09:33
  • @MuhammadHamzaNisar I meant implementing all of Posix on Windows is too broad. I edited the question to reflect what I meant. If you make it more concise I'll remove the down vote. – kabanus Dec 26 '16 at 10:24
  • Windows has pretty complete POSIX support, with `fork` being a notable exception. – IInspectable Dec 26 '16 at 21:21
  • @IInspectable: I don't think that's true. There's a lot of POSIX functions, and while I haven't actually done a survey, I'm fairly sure there's plenty of them that aren't available on Windows, short of installing a third-party library like Cygwin of course. Inter-process signals is another major POSIX feature missing from Windows, also inheritable filenos, the ability to treat pretty much any device exactly as if it were a file, pseudo-terminals, line buffering, using select() on anything other than a network socket, and so on. – Harry Johnston Dec 27 '16 at 00:39
  • @IInspectable [this](http://social.technet.microsoft.com/wiki/contents/articles/10224.posix-and-unix-support-in-windows.aspx) is a good read. The POSIX subsystem provides version POSIX1 support, and is very basic. Another good overview is [here](https://brianreiter.org/2010/08/24/the-sad-history-of-the-microsoft-posix-subsystem/). – kabanus Dec 27 '16 at 08:51
  • @HarryJohnston: I should have said *"substantial POSIX"* support. Besides the POSIX subsystem, there were the SFU, and now there is *Windows Subsystem for Linux*, and Cygwin as a third-party option. An undifferentiated *"there is no POSIX in Windows"* is wrong, no matter how you slice it. If not just for that, this answer received negative feedback for supplying, what is mostly a link-only answer. And the mandatory *"Google is your friend"* statement. This is both wrong as well as not useful. – IInspectable Dec 27 '16 at 13:33
  • @IInspectable I'm pretty sure the edit solved his specific question, unless you mean I should spell out the `exec` line (and maybe a check for the OS)? Otherwise I'm not sure what you expected. If you know of a clean PHP method do go about this you should help the guy out. – kabanus Dec 27 '16 at 13:55
  • This could be **part** of the answer, but you decided to link to off-site content, and never addressed the specific issue, how to map the ID to something, the Windows API can consume. Please see [answer]: *"**Provide context for links:** Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."* – IInspectable Dec 27 '16 at 14:03
  • I thought I did, but I see your point. If you consider this a lazy answer I can understand your reasoning. – kabanus Dec 27 '16 at 14:51
  • This isn't about being lazy or not. It is about providing an answer that remains useful in the future, even when an off-site resource becomes inaccessible. It's no coincidence that Stack Overflow Q&A's show up at the top of internet searches, because they are generally useful, and the rules of this site are part of why this is the case. – IInspectable Dec 27 '16 at 20:13
  • @IInspectable: I don't think WSL and its predecessors (or Cygwin) are relevant to this question, because they aren't reachable from any sort of standard WAMP stack. (Granted, if I'd posted an answer I would probably have mentioned them in a footnote, for the sake of precision. I have no objection to your other points, I was just curious as to what you meant by that one statement.) – Harry Johnston Dec 28 '16 at 01:44
  • @HarryJohnston: You can launch WSL processes from Win32, and communicate with them through network sockets, or VFS file marshalling, so WSL may even be relevant to this question. My main issue with this proposed answer, however, is the blunt statement, that there is no POSIX in Windows. – IInspectable Dec 28 '16 at 02:02
  • @IInspectable Since this is still drawing comments, and since I accepted at least some of your points I have edited my question to list the options you mention, and why I think they are problematic. This may be an opinion, but I stand behind my words that it's probably better to use a native windows C++ piece of code. I removed the "No POSIX!" with a longer paragraph, I hope you feel the quality of this answer has risen at least slightly. – kabanus Dec 28 '16 at 07:34