Hello,
I am new to PHP, but hoping all of you experts out there can help me get better. I have been granted the task of improving our company intranet, and I am trying to give a directory listing of a folder on our network.
I would like to give access to files in this directory for viewing, printing, etc, but not allow them to change them. (also having troubles with permissions here but that is another issue)

Nothing like diving head first into the deep end, with sharks, to learn how to swim! So please bear with me if some of this is obvious or if I am asking something stupid.

With the help of this site I have gotten everything to work except for a couple quarks I can not figure out.

The first thing my page is to do is check if the drive to the directory is mapped. If it isn't I want it to map the drive and then go through a loop to display the contents. A fairly simple 2 step process.

Doing some testing I have gotten both of these tasks to work separately on their own, however things get weird when I tried to combine them.

In the morning when the drive is not yet mapped, when I pull up the page it says it can not read the drive. In other words it seems to skipping the mapping of the drive and going right to the createDir when the drive is not yet mapped.

Here is where it gets weird. If I refresh the page, it works... It maps the drive and lists everything like it should.

I am lost on why it would act this way.
Actually, this just came to me - could it be a cache issue?

Any help is appreciated!

<?php

/* Make sure drive is mapped to directory */

$letter='Q';
$drive= $letter.':\\';

/* Create COM object */
$WshNetwork = new COM("WScript.Network");

if(is_dir($drive))
	{	
		/* Drive is currently mapped, do nothing */
	}
	else
	{	
		/* Map the drive */
		$WshNetwork->MapNetworkDrive( "$letter:" , '\\\server\\folder' , FALSE, 'domain\\user' , 'password' ) or die( 'Unable to map drive');
	}

$path = '//server/folder/';
/*$path = 'Q:\\';   */

function createDir($path = '.')
{	
	if ($handle = opendir($path)) 
	{	
		echo "<ul>";

		while (false !== ($file = readdir($handle))) 
		{
			if (is_dir($path.$file) && $file != '.' && $file !='..')
				printSubDir($file, $path, $queue);
			else if ($file != '.' && $file !='..')
				$queue[] = $file;
		}

		printQueue($queue, $path);
		echo "</ul>";
	}else
	{
		echo "Error reading directoy ".$path; 
	}
}

cont'd...........

    Any reason why you're wanting to use a mapped drive at all in the first place?

      Lol, I figured a reply like this was coming.
      This was the only way I could figure out how to connect to the network directory, however, I am totally open to suggestions.

        The only benefit of using a mapped drive like you are is that PHP can not impersonate another user's credentials.

        Thus, while something like this:

        $file = file_get_contents('//myserver/path/to/file.txt');

        is perfectly valid, it requires that whatever user account invoked the PHP process (e.g. the IIS worker process) must have permissions to access the network share/file in question.

          If I understand you correctly, I can skip the mapping of the drive and just file_get_contents.
          I will just have to be sure that user activating the php (like you said the iis) is added to the permissions of the directory, correct?
          Being that our server is running as admin, and thus iis is running as admin, is there a way to change this so iis is accessing this directory as a more restricted user? I only want the intranet users to have read only access.

          Thanks for the help!

            I could be wrong, but I don't think file_get_contents is what I am looking for.

            I want to be able to read the contents of a folder and the files in it. This command looks like I have to hard code the file I want information on.
            Maybe I am not understanding.

              4 days later

              Update,
              I finally figured it out.

              In my code I included "or die" when I tried to map the drive. The idea ( an may be incorrect) was to produce a recognizable error if something went wrong. Instead, it seems it just printed that error text and ended no matter if it was successful or not.

              So when it got to the IF statement, and needed to map the drive, it would. However, instead of moving on and executing the code to display the directory contents, it just wrote "unable to map drive" and ended.

              I simply removed the "or Die" part of that line, and it works perfectly.

                Write a Reply...