Referencing IIS Virtual Directories with PHP

Posted on 2011-09-27
Medium Priority
Last Modified: 2012-05-12
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?
Question by:cadcollin
  • 5
LVL 11

Expert Comment

by:Paolo Santiangeli
ID: 36711296

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


Author Comment

ID: 36711704
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?
LVL 13

Expert Comment

ID: 36712171

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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 36712593
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).
	font-family:		"Segoe UI", Calibri, Verdana, Arial, sans-serif;
	font-size:			80%;
	text-decoration:	none;
	color:				#33c;
	color:				#c33;
	text-align:			left;
	display:			block;
	clear:				both;
	float:				left;
	padding:			0 1em;
	color:				#999;
	text-align:			right;
	border-bottom:		1px dotted #ccc;
	padding:			0 1em 0 0;
	display:			block;
	margin:				.25em 0;
	background:			yellow;
	// -->
$base_dir = "\\\\server\\PDF_Output\\";
	$_REQUEST["directory_name"] = $base_dir . $_REQUEST["directory_name"];
		if($directory = opendir($_REQUEST["directory_name"]))
			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";
					echo "<h3>There is one PDF in the <i>" . $_REQUEST["directory_name"] . "</i> folder.</h3>\n";
					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";
				echo "<h3>The <i>" . $_REQUEST["directory_name"] . "</i> folder is currently empty.</h3>\n";
		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($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";
		echo "\"$directory_name\" is not a valid directory";

Open in new window


Author Comment

ID: 36719666
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".

Accepted Solution

cadcollin earned 0 total points
ID: 36891609
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.)

Author Closing Comment

ID: 36915472
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.

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question