0

I've been trying to no avail to read either a directory or a file from a network using PHP. The operation works fine if the directories are local but, I have had ZERO luck with the network. My system admin told me that the folders I'm trying to accessed are shared. I can also read the files using python. There are two folders in question I want to access, one with a user name and password while the other one has no protection. For the sake of this particular problem I would be happy just to be able to access the one without the password protection. All it has in it is a junk file that i put there as an experiment. The code in question which is a simple experiment code is found below. The error I received is: Warning: opendir(M:,M:): The system cannot find the path specified. (code:" 3)

<?php  
    $location = "\\\\192.168.0.16\\geo";
    $user = "";
    $pass = "";
    $letter = "M";


    system("net use ".$letter.": \"".$location."\" ".$pass." /user:".$user." /persistent:no>nul 2>&1");


   $dir = $letter . ":";
    #if(is_dir($dir)){
        if($dh = opendir($dir)){
            while(($file = readdir($dh)) !== false){
                echo "filename: $file : filetype: ".filetype($dir . $file) . "\n";
            }
            closedir($dh);
        }
    #}
    ?>
Geo
  • 23
  • 9
  • Take a look here http://stackoverflow.com/questions/14367687/read-file-on-a-network-drive – codelogn Sep 19 '14 at 16:27
  • Nope, I've seen that page before and still no luck. I did tried it again just to make sure. – Geo Sep 19 '14 at 16:56
  • do you see any error with system()? how about trying $result = shell_exec('net use... 2>&1') and print the $result? – codelogn Sep 19 '14 at 19:58

1 Answers1

0

Try to change:

From: $dir = $letter . ":";
To: $dir = $letter . ":/"; // or $dir = $letter . ":\\";

The slash tends to matter sometimes in some cases.

After, try to setup your webserver to accept the directory mapped:

<Directory "M:\Path\To\Your\Directory">
  Order Deny,Allow  
  Allow from all
</Directory>
Fernando
  • 113
  • 5
  • Zero clue. I don't know how to test it. Can you provide me with a sample code. All I want to do is read a file in that geo directory (ip/geo/junk.txt). Any code that can accomplish that would make me very happy. – Geo Sep 19 '14 at 17:26
  • You can check it with something like that: $last_line = system('ls', $retval); echo '
    Last line: '.$last_line.'
    Return val: '.$retval; ?>
    – Fernando Sep 19 '14 at 17:30
  • If nothing works, another option is copy the files to a local path instead of mapping the remote directory. If it's possible... If is, you can copy, process and delete the local files at the end of the process. – Fernando Sep 19 '14 at 17:36
  • I got Last line: RETURN val:1 – Geo Sep 19 '14 at 17:38
  • That will not work because the end goal is to read the content of various files which are generated daily. The name of the files change since are based off a serial number of a machine that is being built on a daily basis. I have all the code to parse the file but, I stumble in this one little problem of not being able to open the darn thing. – Geo Sep 19 '14 at 17:43
  • What the result of is_dir($dir) ? True / False? – Fernando Sep 19 '14 at 17:49
  • Right, you can use var_dump to see false... I realy think it is restriction of the webserver. Like explained here http://stackoverflow.com/questions/10311674/accessing-file-of-another-drive – Fernando Sep 19 '14 at 18:48
  • the "open_basedir" parameter in the php.ini file was commented out. Does php needs a server running in the destination (geo/junk.txt) in order to be able to read the file? The reason i asked is that I was able to read that file using python. – Geo Sep 19 '14 at 19:26
  • No no... I think that the webserver have access to the drive that it is installed and configured. Maybe it's possible to setup to have access to another drives (in the same server/computer not network drives). – Fernando Sep 19 '14 at 21:43
  • Ok, I slightly modify the path slashes a bit and manage to make it work on my work PC that has WAMP on it (Win Xp). However, when I put it on the production server (Win server 2008) it still didn't work. I know now that this is a set up problem and nothing to do with the code unless the versions of php or what not are radically different. Ofcourse this happened at the last 5 minutes of work so I'll have to wait till Monday to investigate further. What I did found odd was that I needed to change the Mapped letter from "M" to an "N" or it wouldn't work. – Geo Sep 20 '14 at 15:37
  • I think is just setup problems at httpd.conf or something like that. Not strict with PHP versions – Fernando Sep 22 '14 at 16:19