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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.


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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses

764 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