• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2495
  • Last Modified:

Referencing IIS Virtual Directories with PHP

We're running PHP v5.3.6 on a Windows Server 2008 R2 (x64) machine with IIS v7.5.  We've created an IIS Virtual Directory pointing to a shared directory on a remote server (via UNC).  With directory browsing enabled (in IIS), we're able to hit the URL of the virtual directory with a web browser and see the files contained within the corresponding directory on the remote server.  However, PHP doesn't seem to be able to utilize the IIS Virtual Directory.

PHP's Filysystem and Directory functions work for local, physical directories, but don't seem to be able to utilize IIS' Virtual Directories.

phpinfo() shows "Virtual Directory Support" is disabled, but from what I've been able to gather, this has more to do with PHP's mod_rewrite URL.


Is there a way to make PHP aware of IIS' Virtual Directories?  If so, how?  And if not, is there an alternative way to reference a remote file system with PHP on IIS?
0
cadcollin
Asked:
cadcollin
  • 5
1 Solution
 
Paolo SantiangeliConsulente InformaticoCommented:
hi,

you can use directly the same unc used in iis to create the virtual dir
es: $filepath = '//serverip/foldername/';

0
 
cadcollinAuthor Commented:
I've tried the following four variants to account for potential syntax issues, but the condition is never satisfied.

if(is_dir("//server/PDF_Output/" . $_REQUEST["directory_name"]))
if(is_dir("//server/PDF_Output/" . $_REQUEST["directory_name"]."/"))
if(is_dir("\\\\server\\PDF_Output\\" . $_REQUEST["directory_name"]))
if(is_dir("\\\\server\\PDF_Output\\" . $_REQUEST["directory_name"]."/"))

($_REQUEST["directory_name"] is set to the name of a directory contained within //server/PDF_Output.)

I've made the share's permissions pretty wide-open, but maybe that's where the issue lies?  What permissions should the remote share have?  I've explicitly extended "Full Control", "Change", and "Read" permissions to "Everyone", "ANONYMOUS LOGON" and the local administrator on the remote server's share.  It seems like that ought to cover the gamut, but maybe the required account is excluded from the "Everyone" group and the "ANONYMOUS LOGON" service/account?
0
 
khairilCommented:
Hi,

It is a security context issues over the shared folder:

Here is what you can try:
Method 1
=======
Make sure your IUSR user has read rights on the folder on the remote server (with same password)

Method 2
=======
Enable Guest acount on remote server and give read access to the shared folder.



0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
cadcollinAuthor Commented:
I've enabled the guest account on the remote server and enabled read permissions -- and write and full control permissions, for now -- for both accounts (IUSR and Guest) on the remote server's share, but PHP still can't see the contents of the remote directory.

The PHP error log isn't generating any errors or warnings.  I'm able to use PHP's Filysystem and Directory functions to reference local directories, so they don't seem to be the issue.

All I'm trying to do is build a list of PDF files contained in a specific remote directory.  Different users are using different directories.

I've attached my code.

Note: I concatenate $base_dir (line 45) and $_REQUEST["directory_name"] to build the UNC path (on line 48).
<html>
<head>
<style>
<!--
body
	{
	font-family:		"Segoe UI", Calibri, Verdana, Arial, sans-serif;
	font-size:			80%;
	}
a
	{
	text-decoration:	none;
	color:				#33c;
	}
a:hover
	{
	color:				#c33;
	}
dt
	{
	text-align:			left;
	display:			block;
	clear:				both;
	float:				left;
	padding:			0 1em;
	}
dd
	{
	color:				#999;
	text-align:			right;
	border-bottom:		1px dotted #ccc;
	padding:			0 1em 0 0;
	display:			block;
	margin:				.25em 0;
	}
.warning
	{
	background:			yellow;
	}
	// -->
</style>
</head>
<body>
<?php
$base_dir = "\\\\server\\PDF_Output\\";
if(isset($_REQUEST["directory_name"]))
	{
	$_REQUEST["directory_name"] = $base_dir . $_REQUEST["directory_name"];
	if(is_dir($_REQUEST["directory_name"]))
		{
		if($directory = opendir($_REQUEST["directory_name"]))
			{
			$count=0;
			while(false !== ($file = readdir($directory)))
				if(filetype($_REQUEST["directory_name"] . "\\" . $file)=="file" && substr($file,-4,4) == ".pdf")
					{
					$warning = (filesize($_REQUEST["directory_name"] . "\\" . $file)<5000?" class=\"warning\"":"");
					$list[$count] = "\t<dt $warning><a title=\"Click here to open " . $file . "\" target=\"pdf\" href=\"file:" . str_replace("\\","/",$_REQUEST["directory_name"]) . "/" . $file . "\">" . str_replace("_","",preg_replace("/(\D+)\d+_(\d+)_\d+/","$2 - $1",substr($file,strpos($file,"_")+1))) . "</a></dt>\n\t\t<dd>" . date("F j, Y, g:i a",filectime($_REQUEST["directory_name"] . "\\" . $file)) . "</dd>\n";
					
					$count++;
					}
			if($count)
				{
				if($count==1)
					echo "<h3>There is one PDF in the <i>" . $_REQUEST["directory_name"] . "</i> folder.</h3>\n";
				elseif($count>1)
					echo "<h3>There are " . number_format($count) . " PDFs in the <i>" . $_REQUEST["directory_name"] . "</i> folder.</h3>\n";
				echo "<dl>\n";
				foreach($list as $listitem)
					echo $listitem;
				echo "</dl>\n";
				}
			else
				
				echo "<h3>The <i>" . $_REQUEST["directory_name"] . "</i> folder is currently empty.</h3>\n";
			closedir($directory);
			}
		}
	else
		echo $_REQUEST["directory_name"] . " is not a valid directory";
	}
else	// If there's no "directory_name" parameter passed in the URL or via HTTP headers.
	{
	$directory_name = "printer_jobs";

	echo "<a href=\"$directory_name\">Click here</a> to see the contents of the IIS virtual directory <i>$directory_name</i>, as seen by IIS.<br /><br />";

	echo "Below is a listing of the contents of the IIS virtual directory <i>$directory_name</i>, as seen by PHP.<br />";
	if(is_dir($directory_name))
		{
		if($directory = opendir($directory_name))
			{
			echo "<ul>\n";
			while(false !== ($file = readdir($directory)))
				echo "\t<li><a href=\"" . $directory_name . "/" . $file . "\">$file</a></li>\n";
			echo "</ul>\n";
			closedir($directory);
			}
		}
	else
		echo "\"$directory_name\" is not a valid directory";
	}
?>
</body>
</html>

Open in new window

0
 
cadcollinAuthor Commented:
Since I haven't been able to make any headway getting PHP to see IIS' Virtual Directory or getting PHP's Directory functions to see the remote share, I decided to try creating a Symbolic Link on the host pointing to the target directory.

Windows Explorer sees the symlinked directory as a sub-directory in my project.  Explorer can read and write to the directory.
IIS Manager sees the symlinked directory as a sub-directory in my project.  In Content View, IIS Manager can browse the directory.
When the symlinked sub-directory's URL is hit with a web browser, I'm greeted with an IIS error page.  Error 401, to be specific, which suggests that some kind of permissions issue may still exist.
PHP can actually see the symlinked directory!  The "is_dir()" check passed, but I still fail-out on the open_dir() function -- but now, at least, PHP is writing warnings to the error log...

The open_dir() call generates two warnings each time the script is executed.  The first warning says that access to the directory was denied, the second just says "failed to open dir: No error in ...index.php".
0
 
cadcollinAuthor Commented:
Instead of accessing the files remotely, I'm now copying the files locally and then referencing them locally with PHP's Filysystem and Directory functions to accomplish what I needed to do.  It works, but it's less-than-ideal -- especially because of the volatility of the content on the remote directory.  (There's frequent changes to the directory through-out the day.)
0
 
cadcollinAuthor Commented:
The end-solution wasn't even one that I wanted to consider early-on, but one that I had to resort to to resolve the problem-at-hand in a reasonable amount of time.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now