Php access network path under windows


Some people maybe confuse about how to access network path from php to manipulate files. There’s an example at http://us.php.net/manual/en/function.opendir.php#90373, how to access your network path. Here’s the code :

<?php

// Define the parameters for the shell command
$location = “\\servername\sharename”
;
$user = “USERNAME”
;
$pass = “PASSWORD”
;
$letter = “Z”
;

// Map the drive
system(“net use “.$letter.“: \””.$location.“\” “.$pass.” /user:”.$user.” /persistent:no>nul 2>&1″
);

// Open the directory
$dir = opendir($letter.“:/an/example/path”
)

?>

You can change the variable code above which is suitable with your network. However, when you try this sample code you’ll get an error : failed to open dir. Then i search the internet and found the solution on http://awesometoast.com/php-and-mapped-network-drives/.

For step no 4. change your current directory through cmd.exe to SysinternalsSuite’s folder where the file psexec exist. And then execute the command below :

psexec -i -s cmd.exe

For step no 5, here is my code :

<?php

// Define the parameters for the shell command
$location = “\\\\servername\\sharefolder”
;
$user = “USERNAME”
;
$pass = “PASSWORD”
;
$letter = “Z”
;
system(“net use “.$letter.”: \””.$location.”\” “.$pass.” /user:”.$user.” /persistent:no>nul 2>&1″);
?>

And here’s the code to print the files listed in the directory :

<?php
if ($dir = opendir($letter.”:”))
{
while (false !== ($filename = readdir($dir))) {
$files[] = $filename;

}
print_r($files);

}

?>

For your information, after you execute the code to map your network drive with php, in windows 7 you can find the disconnect network drive icon under your windows explorer.
explorer
Don’t worry you can dismiss them by restart your laptop, hehehe, since i haven’t found another solution that no need to restart our pc/laptop.

Hope my explanation help you.

 

 

 


3 thoughts on “Php access network path under windows

    1. Warning: opendir(X:/Updated Drawing,X:/Updated Drawing): The system cannot find the path specified. (code: 3) in C:\xampp\htdocs\coba1.php on line 15

      Warning: opendir(X:/Updated Drawing): failed to open dir: No such file or directory in C:\xampp\htdocs\coba1.php on line 15

      1. Di warningnya disebutkan kalau path yang dimaksud tidak ada atau belum dicreate.
        Contoh direktory saya => $dir = “D:/Work/work.php”;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s