5

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.

hungtran
  • 51
  • 1
  • 1
  • 2

2 Answers2

3

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.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • http://stackoverflow.com/questions/2874796/fopen-wont-open-files-across-network This helped me... – Raheel Hasan Jul 11 '13 at 08:53
  • @RaheelHasan you may also find this answer interesting http://stackoverflow.com/questions/14367687/read-file-on-a-network-drive/14367719#14367719 – Lawrence Cherone Jul 11 '13 at 09:31
  • hmm thanks... I did figure that out already... my issue was related to nullSession and was solved by adding login user on Windows Service for Apache.. – Raheel Hasan Jul 11 '13 at 09:42
  • How would you access a file with PHP if you need to enter an username and password? – tedi Dec 09 '16 at 09:22
0

Sounds like you are trying to access a network share (smb) via PHP.

file_get_contents supports only the following protocols.

SMB is not a supported protocol, however there are a few implementations available. The one that keeps coming up in my searches is smbwebclient (untested):

St. John Johnson
  • 6,590
  • 7
  • 35
  • 56