use php read file on network with ip or computername

hungtran picture hungtran · Apr 26, 2012 · Viewed 10.1k times · Source

When i use php read file on LAN network through ip or computer name.

$url = "\\\\192.168.0.200\\testshare\\1.txt";
        if(file_exists($url) == TRUE) {
            echo "OK";
        } else {
            echo "Error";
        }

result: always "Error". If i use function file_get_contents ($url , true); result:

Warning: file_get_contents(\\192.168.0.200\testshare\1.txt) [function.file-get-contents]: failed to open stream: Permission denied in D:\services\htdocs\test.php on line 3

Of course, folder "testshare" is set share and permission everyone full control. If i tape web browser: \192.168.0.200\testshare\1.txt It read content of file 1.txt. Can you help me? Every idea help me, i thanks very much.

Answer

Lawrence Cherone picture Lawrence Cherone · Apr 26, 2012

Here's how you can do it (Win 7) locally, you might have to jump over some firewall hurdles if its a remote server

Create a blank folder on your desktop and put your file in it, then right click it and click share with... then specific people, then in the dropdown options box select everybody.

You will be given an address that looks like testshare (file://YOUR-PC-NAME/Users/YourAcount/Desktop/testshare)

Then just use that address like so:

<?php
$url = "//YOUR-PC-NAME/Users/YourAcount/Desktop/testshare/1.txt";
if(file_exists($url) == TRUE) {
    echo "OK"; 
} else {
    echo "Error";
}
?>

For remote server remember to allow ports 137,445 in your software firewall, also this is a security risk so dont let outside WAN traffic have access. I remember the days when xp had C: drive shared by default.