Hi,

I want to connect to a computer in my network, where the computer is password protected.
So what we normally do is, type \<ip or computer name>. Then when it prompts for username and pass, we provide it in the pop-up window.

So, when we do a opendir('directory name'), it opens normally. but what to do if this ddirectory has some pass.

Please help. This is really urgent and I am stuck up.

My Code is

<?php

function open_dir ($dir_name)
{

if ($handle = opendir ($dir_name)) 

{

while (($file = readdir($handle)) != false)

{

if ($file != '.' && $file != '..')

if (filetype($dir_name.'/'.$file)== 'dir')

{

open_dir ($dir_name.'/'.$file);

}

else

{

echo $file." ";

echo "-------------";

insert_into_database ($dir_name, $file);

}

}

closedir($handle);

}
}

open_dir ("\\\\192.168.1.2\folder");

?>

What to do if "\\192.168.1.2\" computer in the network has password

    paulnaj;10945367 wrote:

    Just googled and found this.

    P.

    Using system/exec() to execute the "net use" command was going to be my suggestion as well.

    dhrubajyoti: Would this solution work for you? If not, it might be possible (and/or better) to create a [man]COM[/man] object and use the MapNetworkDrive method in WScript.Network to map the drive.

    Note, however, that both of these solutions assume that your webserver is running the Windows OS, which might not be true.

      If I type net use Z: \192.168.1.5\Databases\data\account PASSWORD /user:USERNAME /persistent:no in command prompt and then I try to access Z: thorugh COmmand prompt, it works.

      But If I try to do the same using PHP, it says "Warning: opendir(Z🙂 [function.opendir]: failed to open dir: No error in C:\wamp\www\knet\handle.php on line 28"

        Can you show us the code you used? Also, is error_reporting set to E_ALL?

          <?php 
          
          function open_dir ($dir_name)
          {
          	if ($handle = opendir ($dir_name)) 
          	{
          	while (($file = readdir($handle)) != false)
          		{
          			if ($file != '.' && $file != '..')
          			if (filetype($dir_name.'/'.$file)== 'dir')
          			{
          				open_dir ($dir_name.'/'.$file);
          			}
          			else
          			{
          				echo $file." ";
          				echo "-----";
          				//insert_into_database ($dir_name, $file);
          			}
          		}
          	closedir($handle);
          	}
          }
          
          // Define the parameters for the shell command
          /*$location = "\\192.168.1.5\Databases\data\account";
          $user = "";
          $pass = "";
          $letter = "P";*/
          
          // Map the drive
          system('net use P: \\192.168.1.5\Databases\data PASSWORD /user:USERNAME/persistent:no');
          
          //open_dir ('\\\\192.168.1.8\\Utilities');
          open_dir($letter.":")
          
          ?>

          I did not get the meaning of error reporting E_ALL

            In the code you posted, $letter is undefined (it's inside the comment block).

            EDIT: Also note that the backslash has special meaning, so you'll want to escape it:

            'net use P: \\\\192.168.1.5\\Databases\\data PASSWORD /user:USERNAME/persistent:no'

              Oh sorry...
              I just commented the $letter b4 submitting the code here....But Even of we give open_dir("p:") it gives the same error

                See my post above - I noticed a problem with the backslashes.

                  I doubt whether this system () command is getting executed or not...

                    It's possible that PHP doesn't have access to cmd.exe or net.exe on the Windows system, based on the credentials that it's running under. Try something like this instead:

                    $WshNetwork = new COM("WScript.Network");
                    $WshNetwork->MapNetworkDrive("$letter:", '\\\\server\name', FALSE, 'username', 'password');

                      I am getting following error

                      Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> WSHNetwork.MapNetworkDrive<br/><b>Description:</b> A specified logon session does not exist. It may already have been terminated. ' in C:\wamp\www\knet\handle.php:57 Stack trace: #0 C:\wamp\www\knet\handle.php(57): com->MapNetworkDrive('P:', '\192.168.1.5\D...', false, 'USERNAME', 'PASSWORD') #1 {main} thrown in C:\wamp\www\knet\handle.php on line 57

                        FYI...

                        I have created a Z: drive using COmmand prompt. But I cannot enter into this Z: through open_dir("Z:") whereas I can access any other drive by open_dir("D:\ABC\DEF")

                          I'm assuming you replaced 'USERNAME' and 'PASSWORD' with valid credentials?

                          Is this running in an Active Directory domain environment? If so, it might be necessary to use the "domain\user" syntax for the username instead of just "user".

                            Sir,

                            I am pasting the same command in Command promt. Its direct copy paste apart from those backslashes.
                            I did not understand "Active Directory domain environment"

                            Here this ip (192.168.1.5) is my own IP and Database is a folder share by me only on my system.

                            Sir,

                            If possible can you just check with this code in your system...as I think I am stuck up with something strange.

                            I shall be waiting...

                              Hi,

                              I am unable to access z: through opendir() where z: is already created in through command prompt

                                It's most likely a permissions issue, since the PHP parser isn't running under the same user credentials as you are (more than likely, anyway).

                                There's no way to change this; either have the server administrators modify the user credentials that the PHP process uses, or add the credentials that it does use to the ACL for the shared folder to give it access.

                                  So, Sir,
                                  The server being in my local machine, I am the server administrator. So how to modify the rights.

                                  By the way, are you able to reproduce this defect in your local system, or it is working fine for you