PHP Connect to remote computer

I am a beginner in php and want to know if this is possible.
I want to get a file list from a directory on a computer which is connected to the internet. The computer is not running any kind of server like ftp,http etc.
I know its possible to get listing from ftp nlist function but it works only for ftp server.

If the remote computer is not running any kind of server task that will give you a file list, then no, it’s not possible regardless of language, as it is the server task that will provide you with that list of files.

A network file system server might, depending on your access permissions, provide you with a list of files, but then that’s a server task in itself. And keep in mind that the list of files you get from an FTP server will depend on how you log in to that server, so it will almost certainly be a virtual directory and might not even be on the physical computer that the FTP server is running on.

is there any other alternative to doing it? like powershell or something like that

By definition, if there’s something on the remote computer that will sit there and return a directory listing to your enquiry client software, then it’s a server and you were asking how to do it without there being a server in place. So if there’s no server, then there’s nothing on the remote computer will respond to your requests.

Apart from anything else, it’s a terrible security hole. Would you like to think that every time you connect to the net, someone can run a bit of code that gets your directory listing? Of course not, so you turn off stuff that would allow it, and firewall it so that unauthorised users can’t access to the stuff you need to keep running.

1 Like

As droopsnoot said, you cannot get list of files from the remote computer.
What you actually can is to ask that computer to give you the list and then read it.
That means, there should be something running on the remote computer and listening for your requests (i.e. server).

is it possible to connect to a pc running windows server 2008 R2? using authentication?

if somebody GRANTED you access through a SERVICE, then yes. like Windows IIS.

Is there any tutorial or something for PHP with windows server, I cant seem to find it on the web.Most of them are for SQL server but I need for file system.

for which service?

I am not very sure about services but what service is required to get contents of a folder?

FTP is probably the easiest… Oh, hang on.

I believe that the Microsoft implementation of a network file system isn’t publicly documented, so if that’s the target system you may have to look at how some of the open-source systems handle it. Though I seem to recall reading that it was sufficiently complex (and ever-changing) that many now concentrate on providing their own servers to share files.

But if it’s a system that wants you to gain access to its file system, surely the owner of that system can provide a means to grant that access? And if it isn’t, you won’t get past the firewall.

Any other way than hosting a FTP server on the windows server? Because there is not just one server. I need to get the contents of the folder from multiple servers with windows server 2008 R2.

Ok I found out that the server data is actually shared over network and is accessible with \\192.168.xx.xx. So if I want to get directory list can I use this
(assuming I have access to it)

$dir = "\\192.168.xx.xx\";
$a = scandir($dir);
print_r ($a);

so your server provides SMB protocol service

Yes I guess they do.

Well, that’s a server, so it’s a bit outside of the scope of the original question. But if it does what you want, that’s great. Keep in mind that the directory you receive won’t necessarily be the physical directory - from what I recall, shares on an SMB / NTFS server are similar to FTP in that they provide access to a virtual directory space.

Actually I found out later that the server is shared on network and a windows server was running on it. Sorry for the inconvenience. Thanks for replying

No problem, glad it’s doing what you need it to.

Well I have not tested it yet but I guess UNC Path should work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.